iPodMusicPlayer playbackState inaccurate after odd conditions

Posted by Shane Costello on Stack Overflow See other posts from Stack Overflow or by Shane Costello
Published on 2011-12-15T02:51:33Z Indexed on 2012/04/12 17:29 UTC
Read the original article Hit count: 247

I have a simple music player app that runs into a really weird problem. First of all, while playing music and in the locked state, I allow the user to double click the Home button and use the locked iPod music controls. I did notice however that while in the locked state, my app doesn't receive any of its registered notifications. For the most part, this is fine anyway. But, if the user is playing music for at least 15 minutes (I'm not sure why but any less and this problem doesn't occur) while in the locked state, and using some kind of headphone or aux jack, then unplugs the headphone/aux jack while the device is still playing music, the iPodMusicPlayer will auto-pause. Which is exactly what I would want it to do, but after this happens, when the user unlocks their device and gives focus to the app again, the iPodMusicPlayer's playbackState is inaccurate.

- (IBAction)playPause:(id)sender {
     if ([musicPlayer playbackState] == MPMusicPlaybackStatePlaying) {
         [musicPlayer pause];    
     } else {
         [musicPlayer play];
     }
 }

where musicPlayer = [MPMusicPlayerController iPodMusicPlayer].

Under normal circumstances, this runs perfectly fine. But after these conditions, my breakpoint will hit the condition for MPMusicPlaybackStatePlaying while the music is paused, and vice versa. The only way I've been able to fix this is to either make a new selection of music or to terminate the app and reopen. I've tried tons of a workarounds to fix this problem programmatically, but nothing turns out a 100% bug free fix. Does anyone have any clue as to why this happens in the first place?

© Stack Overflow or respective owner

Related posts about music

Related posts about state