使用AppDelegate,我们会回复通知:
applicationWillResignActive 和 applicationDidBecomeActive
在我们的主程序中,上述调用分别触发 saveAudioState 和 restoreAudioState 。它们抓住了AudioPlayer的状态,然后在我们从中断回来时恢复。这可以在电话中断时正常工作,但不能按下主页按钮。
在Home按钮的情况下,currentTime和currentAudioItem的值无效。
- (void) saveAudioState {
currentAudioItem = musicPlayer.nowPlayingItem;
currentTime = musicPlayer.currentPlaybackTime;
NSLog(@"Playback state %d", musicPlayer.playbackState);
NSLog(@"Time %f", currentTime);
NSLog(@"Track %@", currentAudioItem);
audioWasPlaying = NO;
if (musicPlayer.playbackState == MPMusicPlaybackStatePlaying) {
audioWasPlaying = YES;
[musicPlayer pause];
}
}
- (void) restoreAudioState {
// Restore the now-playing item and its current playback time.
musicPlayer.nowPlayingItem = currentAudioItem;
musicPlayer.currentPlaybackTime = currentTime;
// If the music player was playing, get it playing again.
if (audioWasPlaying && currentAudioItem) {
[musicPlayer play];
}
}
这是iOS的错误还是导致事情被拆除的结果?
我已在iOS 5.1和5.1.1上测试过。