问题描述:
游戏中暂停后,调用AVAudioPlayer的pause方法,发现返回游戏后音乐还在播放。
问题解决:
参考http://stackoverflow.com/questions/6403261/avaudioplayer-keeps-playing-after-multitasking
附:
I have spent hours today with precisely the same problem. I finally have a workaround.
The #stop method releases the hardware, but doesn't forget the current playback position.
So you can call #play again and playback resumes as desired.
My previous code was:
[player pause]; ... if ([player isPaused]) { [player play]; }
This of course didn't work, and playback happily resumed after the app came back into the foreground, as you have experienced.
Now I have something like this:
self.playbackSuspended = YES; [player stop]; ... if (self.playbackSuspended) { [player play]; }
A hack, but it works. The only drawback is that #prepareToPlay is implicitly called to regain ownership of the playback hardware, re-buffer, etc. so there might be a delay in playback - I have not noticed one so far.
Also note that depending on your implementation, handling interruptions such as phone calls may be broken after the above hack. Phone call interruptions behave correctly, so by hacking like above, any shared code will likely be in a confused state for interruption handing.