Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

AVQueuePlayer

Hi guys. Am trying to stream mp3 songs using AVQueuePlayer. I can play the songs well but am stuck on how I can fetch the title of the current playing song and update the UI. Below is the code snippet on how I am playing the songs.

   NSArray *trackDataArray = [trackJSON objectForKey:TRACKS_KEY];

            musicArray = [NSMutableArray array];

            dispatch_async(dispatch_get_main_queue(), ^{

                for ( NSDictionary *trackDic in trackDataArray )
                {

                    AVPlayerItem *thePlayerItem = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:[trackDic objectForKey:TRACK_URL_KEY]]];

                    [musicArray addObject:thePlayerItem];
                }

                audioPlayer = [AVQueuePlayer queuePlayerWithItems:musicArray];

                [audioPlayer play];

                [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerItemDidReachEnd:)
                                                             name:AVPlayerItemDidPlayToEndTimeNotification
                                                           object:[musicArray lastObject]];

Also I would like to reset the player so that when the end of my music list is reached.

 -(void)playerItemDidReachEnd:(NSNotification *)notification
{

    //Reset the player to the first item in the array    
}

I will really appreciate any help. Below is an example of the json response am using

{
tracks:
  [
     {
         title: "Matter of time",
         artist: "Anna Lena",
         trackUrl: "http://..../song.mp3"
     },
  ]
}