MPMusicPlayerController applicationMusicPlayer - 在应用程序启动时恢复

时间:2012-10-11 12:58:39

标签: ios mpmusicplayercontroller

MPMusicPlayerController applicationMusicPlayer独立于iPod(或音乐)应用运行,这就是我想要的。当应用程序进入后台时它会停止播放,这也是我想要的。

但是,我希望它在应用程序恢复时继续播放。目前我通过再次调用play方法来执行此操作,但这会导致音频从头开始。有没有办法我可以从音频停止的地方继续播放?我似乎无法找到任何相关信息。

1 个答案:

答案 0 :(得分:4)

在App Delegate的applicationWillResignActive:方法内,阅读musicPlayer.currentPlaybackTime属性(在MPMediaPlayback协议中声明)并将该值存储在某处。您可以设置如下属性:

@property (assign, nonatomic) NSTimeInterval playbackTime;

并指定:

- (void)applicationWillResignActive:(UIApplication *)application
{
    MPMusicPlayerController *myPlayer =
    [MPMusicPlayerController applicationMusicPlayer];

    self.playbackTime = myPlayer.currentPlaybackTime;
    [myPlayer pause];
}

当应用程序即将再次激活时,将调用App Delegate的applicationDidBecomeActive:方法。在该方法中,设置currentPlaybackTime属性。

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    MPMusicPlayerController *myPlayer =
    [MPMusicPlayerController applicationMusicPlayer];
    myPlayer.currentPlaybackTime = self.playbackTime;
    [myPlayer play];
}