2017-09-10 2 views
0

MP3 HTTP 스트림에서 메타 데이터를 가져오고 싶습니다. 스트림은 AVPlayer로 재생됩니다.MP3 HTTP 스트림에서 메타 데이터 가져 오기

AVPlayerItemMetadataCollector를 사용해 보았지만 nil을 반환합니다.

대신 현재 제목이 표시되는 스트림의 웹 사이트에서 특정 부분을 가져 오는 것입니다.

let playerItem = AVPlayerItem(url: URL(string: "stream.p4.no/p5trondheim_mp3_hq")!) 
player = AVPlayer(playerItem: playerItem) 
player.rate = 1.0; 
player.play() 

내가 무엇을 할 수 있습니다 : 여기

내 플레이어 코드?

+0

당신이 당신의 질문에 조금 더 많은 예제 코드를 추가 할 수 있습니까? – ventiseis

+0

내 플레이어 코드는 다음과 같습니다 –

+0

let playerItem = AVPlayerItem (url : URL (문자열 : "http://stream.p4.no/p5trondheim_mp3_hq")) player = AVPlayer (playerItem : playerItem) player.rate = 1.0; player.play() –

답변

1

당신은 당신의 AVPlayerItem에 timedMetadata을 관찰 할 수있다 :

{ 
    ... 
    // Add observer 
    playerItem.addObserver(self, forKeyPath: "timedMetadata", options: .new, context: nil) 
    ... 
} 

// Observe 
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { 
    guard keyPath == "timedMetadata" else { return } 
    guard let meta = playerItem.timedMetadata else { return } 
    for metadata in meta { 
    if let songName = metadata.value(forKey: "value") as? String { 
     print("song name is '\(songName)'") 
    } 
    } 
} 
+0

답변 해 주셔서 감사합니다. 지금까지는 작동했지만 String에서 특정 부분을 어떻게 필터링 할 수 있습니까? 그것은 반환합니다 : 노래 이름은 '{ "artist": "Alan Walker feat. Gavin James", "duration": 179041, "imageUrl": "http://trondheim.p5.no/mmo/MusicLibrary/14c1b7c2-92e4- 4a7e-be70-90ca7112f2c5.jpg ","MetaContentId ": 9314462,"musicId ":"1720-03 ","title ":"피곤한 ","유형 ":"음악 ","url ":"http : // trondheim.p5.no/musikk/sang/1720-03 "} ' –

관련 문제