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)
Asked
Active
Viewed 1,502 times
1 Answers
7
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
.

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