Search code examples
iosmpmovieplayercontrollerxcode4.2http-live-streaming

Getting MetaData from MPMoviePlayerController


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.


Solution

  • 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!