Question

I'm using the MPMusicPlayerController to play MPMediaItems in a MPMediaItemCollection. How can I fire an event when the MPMediaItems are done playing?

Thanks! interdev

Was it helpful?

Solution

Register for MPMusicPlayerControllerPlaybackStateDidChangeNotification notifications:

[notificationCenter addObserver:self selector:@selector(handlePlaybackStateChanged:) name:MPMusicPlayerControllerPlaybackStateDidChangeNotification object:self.musicPlayer];

and tell your musicPlayerController to generate those notifications:

[self.musicPlayerController beginGeneratingPlaybackNotifications];

In handlePlaybackStateChanged: you can check the playbackState property of musicPlayerController:

- (void)handlePlaybackStateChanged:(NSNotitication*)notification
{
    if (self.musicPlayerController.playbackState == MPMusicPlaybackStateStopped ||
        self.musicPlayerController.playbackState == MPMusicPlaybackStateInterrupted ||
        self.musicPlayerController.playbackState == MPMusicPlaybackStatePaused) {
        // do your stuff
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top