由Home按钮中断的MPAudioPlayerController无法保存状态

时间:2012-09-18 02:34:39

标签: objective-c ios5.1 mpmusicplayercontroller

使用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上测试过。

0 个答案:

没有答案