Search code examples
iosxcodeavplayeritem

Stream data (Artist / Title) not displaying in AVPlayerItemTrack array in xcode


I have the following code which, when used on most (Shoutcast) streaming servers, returns the artist/song metadata, but on a particular server it is not working and is simply displaying the letter 't'!

As NSLog isn't currently working for me (despite all kinds of changes, I can't get it to work (Xcode 9.2)), I'm slightly in the dark.

- (void)FetchMeta
{
    for (AVPlayerItemTrack *item in player.currentItem.tracks)
    {
        if ([item.assetTrack.mediaType isEqual:AVMediaTypeAudio])
        {
            NSArray *meta = [playerItem timedMetadata];
            for (AVMetadataItem *metaItem in meta)
            {
                NSString *source = metaItem.stringValue;
                if(![source isEqualToString:self.songInfo.text])
                {
                    self.songInfo.text = [source stringByAppendingString:@"  "];
                    //self.songInfo.text = @"Test";
                    [self LoadArtistImage];
                }
                NSLog(@"meta %@\n%@",source,metaItem.extraAttributes);
            }
        }
    }
}

I know I'm in the right part of the code, because if I uncomment the //self.songInfo.text = @"Test"; line, I see it on the display where the song metadata should appear, but have been on this for 3 hours solid and can't get it to display the metadata and not having NSLog really isn't helping.

UPDATE : I have been able to interrogate the servers and can see the difference, but I still no not know how to tell the code to look in correct field... My understanding is that the data is being put into an array (I think) but it is taking the wrong field. Maybe it is always just taking the last field? TO be clear, I'm looking for it to take the 'StreamTitle' field.

Working server response:

Input #0, mp3, from 'http://server:port':
  Metadata:
    icy-notice1     : <BR>This stream requires <a href="http://www.winamp.com">Winamp</a><BR>
    icy-notice2     : SHOUTcast DNAS/posix(linux x86) v2.5.1.724<BR>
    icy-name        : Station Name
    icy-genre       : 80s, Pop
    icy-br          : 128
    icy-sr          : 44100
    icy-url         : http://website.com
    icy-pub         : 1
    StreamTitle     : Rod Stewart - Lost In You

Non working server response:

Input #0, mp3, from 'http://server:port/':
  Metadata:
    icy-notice1     : <BR>This stream requires <a href="http://www.winamp.com/">Winamp</a><BR>
    icy-notice2     : SHOUTcast Distributed Network Audio Server/Linux v1.9.8<BR>
    icy-name        : Station Name
    icy-genre       : Various
    icy-url         : www.website.com
    icy-pub         : 1
    icy-br          : 128
    StreamTitle     : Theory of a Deadman - Rx
    StreamUrl       : t

To further clarify. The 'working server' output works correctly displaying the 'StreamTitle' field using the code displayed, the non working server displays the last field 'StreamUrl' which is not the desired field.

Any pointers?

Many thanks.


Solution

  • As @Larme says your loop will essentially set your text field to the value of each item in turn, so the value of the last item "wins".

    You have to only set your text field if the current item in your loop is the stream title, something like that:

            for (AVMetadataItem *metaItem in meta)
            {
                if([metaItem.key isEqualTo: AVMetadataIcyMetadataKeyStreamTitle])
                {
                    self.songInfo.text = metaItem.stringValue;
                }
            }