Getting MetaData from MPMoviePlayerController
Asked Answered
S

1

6

Ok So i have a Live Stream from a URL using MPMoviePlayerController.

Player = [[MPMoviePlayerController alloc] 
               initWithContentURL:[NSURL URLWithString:@"MY_URL_HERE_I_REMOVED"]];
Player.movieSourceType = MPMovieSourceTypeStreaming

Now The stream gives Meta Data (I believe thats what everyone calls it). Eg. Name of the track etc.

I want to get this information and display it on a Label.

I have no idea how to get it, I cant change from MPMoviePlayerController and after searching for hours i found MPTimedMetadata class reference but dunno how to use to get this information.

Great if you can mention how to use the notification also to trigger every time this data changes.

Sinless answered 23/12, 2011 at 14:14 Comment(0)
F
15

Assuming that you already know what kind of metadata is being sent from the stream (if you don't, use a media player like VLC to see), you must first register a notification to get the metadata in timed intervals and then a method to process them.

Starting with the notification, just

 [[NSNotificationCenter defaultCenter] addObserver:self
                                          selector:@selector(MetadataUpdate:)
                                              name:MPMoviePlayerTimedMetadataUpdatedNotification
                                            object:nil];

after the MPMoviePlayerController allocation.

Then on the MetadataUpdate method

- (void)MetadataUpdate:(NSNotification*)notification
{
    if ([streamAudioPlayer timedMetadata]!=nil && [[streamAudioPlayer timedMetadata] count] > 0) {
        MPTimedMetadata *firstMeta = [[streamAudioPlayer timedMetadata] objectAtIndex:0];
        metadataInfo = firstMeta.value;
    }
}

where streamAudioplayer is your MPMoviePlayerController and metadataInfo a NSString to store the value. The above will get the Artist and Track info of the currently playing song.

This is the case for the standard metadata send by a shoutcast or icecast stream. (can't say for others because I haven't tried them)

Note that each stream may handle and send different metadata. Since [streamAudioPlayer timedMetadata] is a NSArray you can

NSArray *metaArray = [streamAudioPlayer timedMetadata];
NSLog (@"%i", [metaArray count]); //to see how many elements in the array
MPTimedMetadata *firstMeta = [[streamAudioPlayer timedMetadata] objectAtIndex:0];

Use then the debug console to show the content of metadata using the key, keyspace, timestamp, value properties.

All the above is just an example. There isn't a single way to handle metadata. Detailed information can be found at

https://developer.apple.com/library/ios/#DOCUMENTATION/MediaPlayer/Reference/MPTimedMetadata_Class/Reference/Reference.html

for the MPTimedMetadata class reference and from there... code on!

Frayne answered 15/3, 2012 at 10:16 Comment(2)
Hi... four years later, but still, may I ask... is there any case where the timedMetadata method returns an array with more than one MPTimedMetadata instance? Right now I'm using a for-in loop for getting all of them, but I'm wondering if that's really necessary instead of just using the first element of the array. Thanks!Knowable
Technically speaking... It could. This would require either a custom-compiled Icecast version or a new Shoucast/Icecast release that changes this. On special cases, it might return a second object with probably the web address of the station. In my experience, I am 100% certain that standard Shoutcast/Icecast servers will have the metadata on the first (index 0) object.Frayne

© 2022 - 2024 — McMap. All rights reserved.