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)
MPMusicPlayerController
has an instance method beginGeneratingPlaybackNotifications()
. There are three Notification
s 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 MPMusicPlayerController
s 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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With