AvPlayer streaming, all the metadata information are in timedMetadata?
avplayer, ios, stream, streaming
Solution
I tried to find best solution to get metadata from AVPlayer and found it:
-(IBAction) BtnGoClick:(id)sender {
NSURL *url = [[NSURL alloc] initWithString:@"http://cast.loungefm.com.ua/loungefm"];
[self setupAVPlayerForURL:url];
}
-(void) setupAVPlayerForURL: (NSURL*) url {
AVAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
AVPlayerItem *anItem = [AVPlayerItem playerItemWithAsset:asset];
player = [AVPlayer playerWithPlayerItem:anItem];
[player addObserver:self forKeyPath:@"status" options:0 context:nil];
[anItem addObserver:self forKeyPath:@"timedMetadata" options:NSKeyValueObservingOptionNew context:NULL];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (object == player && [keyPath isEqualToString:@"status"]) {
if (player.status == AVPlayerStatusFailed) {
NSLog(@"AVPlayer Failed");
} else if (player.status == AVPlayerStatusReadyToPlay) {
NSLog(@"AVPlayer Ready to Play");
} else if (player.status == AVPlayerItemStatusUnknown) {
NSLog(@"AVPlayer Unknown");
}
}
if ([keyPath isEqualToString:@"timedMetadata"])
{
AVPlayerItem* playerItem = object;
for (AVMetadataItem* metadata in playerItem.timedMetadata)
{
if([metadata.commonKey isEqualToString:@"title"]){
NSLog(@"%@",metadata.stringValue);
}
}
}
}
Result:
Problem
I'm creating an ios app with a streaming player maded with AvPlayer. This is my code: ``` - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [self InitPlayer]; [self ReadMetaData]; } -(void)InitPlayer{ NSURL *url = [[NSURL alloc] initWithString:@"http://www.fakeurl.com/stream"]; // create a player view controller self.player = [AVPlayer playerWithURL:url]; player.closedCaptionDisplayEnabled = NO; } -(void)ReadMetaData{ [self.player.currentItem addObserver:self forKeyPath:@"timedMetadata" options:NSKeyValueObservingOptionNew context:nil]; } - (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) { if([metadata.commonKey isEqualToString:@"title"]){ TitleLabel.text=metadata.stringValue; } } } } ``` With this code i can successfully play and pause the stream. I can also print the title of the track. If i try to log the timedMetadata with something like this: ``` NSLog(@"%@",player.currentItem.timedMetadata); ``` I retrive that: ``` "<AVMetadataItem: 0x15649500, identifier=common/title, keySpace=comn, key class = __NSCFConstantString, key=title, commonKey=title, extendedLanguageTag=(null), dataType=(null), time={21888/44100 = 0.496}, duration={INVALID}, startDate=(null), extras={\n}, value=Keepin-'fake song title>" ``` Now my question is: for that specific stream url the timedMetadata i logged are the only metadata i can retrive? If yes how i can achive a more complex player type (something like "go to next track button","go to previous track button","an history of tracks",ecc...) ? That's the first time i work with stream data and in my expectations there was a lot of information in audio metadata. In real life seems i can get only the track title. There's a problem with my code or the stream source is poor in metadata info?