Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 309
Richard Hughes Avatar asked Mar 08 '23 05:03

Richard Hughes


1 Answers

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

  1. MPMusicPlayerControllerNowPlayingItemDidChange
  2. MPMusicPlayerControllerPlaybackStateDidChange
  3. 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.

like image 61
naglerrr Avatar answered Mar 19 '23 20:03

naglerrr