Reputation: 21
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?
Upvotes: 2
Views: 1249
Reputation: 2579
This problem still persists in iOS 8. I tried the method @matbur suggested, it worked for a little while, but stopped working at some point. So I bypassed the whole problem by keep track of the state myself:
- (void)viewDidLoad {
self.isMusicPlaying = NO;
}
- (void)pauseAllSound {
self.isMusicPlaying = NO;
[self.musicController pause];
}
- (void)playAllSound {
self.isMusicPlaying = YES;
[self.musicController play];
}
Upvotes: 0
Reputation: 2794
Same kind of problem on my side. It seems that putting the play call on the run loop's queue solves this issue (at least in my context). You can try it with this line of code:
[musicPlayer performSelector:@selector(play) withObject:nil afterDelay:0];
Upvotes: 0
Reputation: 41
You can also try this workaround:
- (IBAction)playPause:(id)sender {
if ([musicPlayer playbackState] == MPMusicPlaybackStatePlaying) {
[musicPlayer play];
[musicPlayer pause];
} else {
[musicPlayer pause];
[musicPlayer play];
}
}
Upvotes: 3
Reputation: 41
I am having this exact same problem, in fact i found more ways to get the problem, and the most effective one is to put the app in background, and from the ipod player use the play/pause button several times very fast. in 50% of the cases, the states gets unsynced. I´ve also tried lots of workarounds, but there is no way to getting my app to work. I think there is a bug in the iOS framework, as the docs havent changed in any way.
I came to a workaround for this case which is detailed here: Other StackOverflow question
Upvotes: 1