Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS7 MPMusicPlayerController states incorrect

I have an iOS7 application with music player. I'm using the following code to check playback state changes from the MPMusicPlayerController to update the UI. More precisely I toggle the look of the play button between play and pause.

[[NSNotificationCenter defaultCenter] addObserver: self
                   selector: @selector (handle_NowPlayingItemChanged:)
                       name: MPMusicPlayerControllerNowPlayingItemDidChangeNotification
                     object: self.musicPlayer];

[[NSNotificationCenter defaultCenter] addObserver: self
                   selector: @selector (handle_PlaybackStateChanged:)
                       name: MPMusicPlayerControllerPlaybackStateDidChangeNotification
                     object: self.musicPlayer];

[self.musicPlayer beginGeneratingPlaybackNotifications];

if I run the app on iOS7 on an iPad or iPhone, I get the following sequence instead of just one single callback:

-[MyMediaPlayer handle_PlaybackStateChanged:] :: playbackState: 1
-[MyMediaPlayer handle_PlaybackStateChanged:] :: playbackState: 2
-[MyMediaPlayer handle_PlaybackStateChanged:] :: playbackState: 1
-[MyMediaPlayer handle_PlaybackStateChanged:] :: playbackState: 2

playbackState 2 means MPMusicPlaybackStatePaused and causes my application to display the wrong state in the UI, while the song is actually being played back. It doesn't make sense that the callback gets called 4 times, with alternating values.

This happens when changing the queue only. It looks like the system is not clearing the queue properly.

Any ideas on how to solve this ?

like image 922
swifferina Avatar asked Sep 20 '13 06:09

swifferina


2 Answers

I've been experiencing the same problem since the release of iOS 7. It's defeinitely a problem with iOS; not only is it reporting an incorrect playback state but also on occasions fails to receive playback state change notifications (MPMusicPlayerControllerPlaybackStateDidChangeNotification)

I've reported the bug to Apple and I suggest you do the same thing (Apple Bug Reporter)

like image 80
sooper Avatar answered Oct 12 '22 14:10

sooper


I had the same problem. I found this workaround, instead of use playbackState to check if the audio is playing, I used this condition:

if([[AVAudioSession sharedInstance] isOtherAudioPlaying])

like image 35
Hugo Vanderlei Avatar answered Oct 12 '22 15:10

Hugo Vanderlei