Question

This seems like something really basic but how do I execute a function when a song ends? I am playing one item at at time, and when it ends (or if possible, 10 seconds before it ends) I would like to call a function that gets the next song from a webservice.

I think it's handled in the MPMusicPlayerControllerPlaybackStateDidChangeNotification notification but am not sure. For the record, this notification only needs to be when the song ends (not pauses).

All I have is this, which is the function for the above observer:

- (void) handle_PlaybackStateChanged: (id) notification
{
    MPMusicPlaybackState playbackState = [musicPlayer playbackState];
}

How do I find out if the song has ended? Also, is it possible create an observer/function when there are 10 seconds left on the song?

Was it helpful?

Solution

When you receive the notification you can look up the playbackState property of the MPMusicPlayerController.

As for executing a function 10 seconds before the song ends, you could set up a timer to execute in the future based on the length of the song minus 10 seconds, you would need to cancel that timer if the user changes the playbackState or selects a different song.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top