4

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)

1 Answers1

7

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.

naglerrr
  • 2,809
  • 1
  • 12
  • 24
  • Hello! I know it is an old answer but I want to ask something about MPMusicPlayer. Is MPMusicPlayer can control when music listening from the apple music app? Actually, I want to learn; is MPMusicPlayerController only using the device's Music app library or it can also have the capability to use and control Apple music? – Burak Kurhan Aug 01 '22 at 08:17