is it possible to read metadata using HTTP live streaming in the iPhone SDK

后端 未结 4 879
被撕碎了的回忆
被撕碎了的回忆 2021-01-31 12:40

When playing a live stream using the HTTP Live Streaming method, is it possible read the current metadata (eg. Title and Artist)? This is for an iPhone radio app.

4条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-31 12:44

    Not sure that this question is still actual for its author, but may be it will help someone. After two days of pain I investigated that it's quite simple. Here is the code that works for me:

    AVPlayerItem* playerItem = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:]];
    
    [playerItem addObserver:self forKeyPath:@"timedMetadata" options:NSKeyValueObservingOptionNew context:nil];
    
    AVPlayer* player = [[AVPlayer playerWithPlayerItem:playerItem] retain];
    [player play];
    

    and then:

    - (void) observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object
                            change:(NSDictionary*)change context:(void*)context {
    
       if ([keyPath isEqualToString:@"timedMetadata"])
       {
          AVPlayerItem* playerItem = object;
    
          for (AVMetadataItem* metadata in playerItem.timedMetadata)
          {
             NSLog(@"\nkey: %@\nkeySpace: %@\ncommonKey: %@\nvalue: %@", [metadata.key description], metadata.keySpace, metadata.commonKey, metadata.stringValue);
          }
       }
    }
    

    That's it. I dont know why Apple didn't provide in the docs for AVPlayerItem this sample for access "title" of the stream which is the key feature for real world streaming audio. In "AV Foundation Framework Reference" they tell about "timedMetadata" nowhere where needed. And Matt's sample does not work with all streams (but AVPlayer does).

提交回复
热议问题