Q:

Detecting When An Apple Music Song Has Finished Playing

I'm writing an app that needs to enact an action when a song has finished playing. I'm using MPMusicPlayerController.applicationQueuePlayer() as my music player to play the user's apple music. I was wondering if there was a way I could detect when a users song has finished playing or when the queue has finished (either would be helpful) 

A:

MPMusicPlayerController has an instance method beginGeneratingPlaybackNotifications(). There are three Notifications that will be delivered to subscribers:

MPMusicPlayerControllerNowPlayingItemDidChange
MPMusicPlayerControllerPlaybackStateDidChange
MPMusicPlayerControllerVolumeDidChange

To detect when playing a song or a queue has finished, you can use MPMusicPlayerControllerNowPlayingItemDidChange. 
When you receive that notification, check the MPMusicPlayerControllers nowPlayingItem (see Documentation). If the song finished and another one is played nowPlayingItem will have changed. If the whole queue finished and there is nothing playing, nowPlayingItem will have a value nil.

