Retrieving Metadata from MPMoviePlayerController

So, I have a direct stream from the url using MPMoviePlayerController.

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

Now the stream transfers metadata (I believe that everyone calls it). For instance. Track title, etc.

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

I have no idea how to get this, I can’t change it from MPMoviePlayerController, and after searching for hours, I found a link to MPTimedMetadata strong>, but I don’t know how to use this information.

It’s great if you can mention how to use the notification, also run it every time this data changes.

+6
source share
1 answer

Assuming that you already know what metadata is sent from the stream (if you do not, use a media player such as VLC), you must first register a notification to receive metadata at time intervals, and then the method for processing it.

Starting from the notice, just

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

after allocating MPMoviePlayerController.

Then in 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 is NSString to store the value. The above information will contain information about the artists and tracks of the current song.

This refers to sending standard metadata to a shoutcast or icecast stream. (I can not say for others, because I have not tried them)

Please note that each stream can process and send different metadata. Since [streamAudioPlayer timedMetadata] is an 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 the debug console to display metadata content using keys, keys, timestamps, value properties.

All of the above is just an example. There is more than one way to process metadata. Detailed information can be found at

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

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

+14
source

Source: https://habr.com/ru/post/904392/


All Articles