I have an application that plays music.
I use the following code to listen for playback status changes from MPMusicPlayerController to update the user interface. More precisely, I switch the appearance of the play button between play and pause.
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter]; [notificationCenter addObserver: self selector: @selector (handle_NowPlayingItemChanged:) name: MPMusicPlayerControllerNowPlayingItemDidChangeNotification object: self.musicPlayer]; [notificationCenter addObserver: self selector: @selector (handle_PlaybackStateChanged:) name: MPMusicPlayerControllerPlaybackStateDidChangeNotification object: self.musicPlayer]; [self.musicPlayer beginGeneratingPlaybackNotifications];
This works great on iPod Touch (iOS 5) and iPhone 3GS (iOS 5) . Each time the playback state changes, I get the following callback:
[JBMediaPlayer handle_PlaybackStateChanged:] :: playbackState: 1
where 1 means MPMusicPlaybackStatePlaying .
However, if I run the same on iPad 1 (iOS 5) , iPad 2 (iOS 5) or iPad 3 (iOS 6) I get the following sequence, and not just one callback:
-[JBMediaPlayer handle_PlaybackStateChanged:] :: playbackState: 1 -[JBMediaPlayer handle_PlaybackStateChanged:] :: playbackState: 2 -[JBMediaPlayer handle_PlaybackStateChanged:] :: playbackState: 1 -[JBMediaPlayer handle_PlaybackStateChanged:] :: playbackState: 2
where 2 means MPMusicPlaybackStatePaused and causes my application to display an incorrect state in the user interface because the song is actually playing.
The funniest thing is that from time to time the sequence
-[JBMediaPlayer handle_PlaybackStateChanged:] :: playbackState: 1 -[JBMediaPlayer handle_PlaybackStateChanged:] :: playbackState: 2 -[JBMediaPlayer handle_PlaybackStateChanged:] :: playbackState: 1 -[JBMediaPlayer handle_PlaybackStateChanged:] :: playbackState: 2 -[JBMediaPlayer handle_PlaybackStateChanged:] :: playbackState: 1
which ends correctly with 1 MPMusicPlaybackStatePlaying , however it still does not make sense that the callback is called 5 times with variable values.
Any ideas on how to solve this or suggest, what else can I check to narrow down the problem?
Since I have not received an answer yet, I also cross-posted the question on the Apple Developer Forum : https://devforums.apple.com/thread/158426