MPMusicPlayerController在后台暂停,当应用程序进入前台时恢复

时间:2012-04-08 13:38:15

标签: iphone background resume mpmusicplayercontroller

我已经实现了MPMusicPlayerController来播放来自库的音乐。当应用程序进入后台时,我暂停它,当它回到前台时,我希望它恢复。它暂停,但从一开始就开始。继承人代码......

AppDelegate.m

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    [self.appMusicPlayer pause];
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    [self.appMusicPlayer play];
}

MainViewController.m

- (void) mediaPicker: (MPMediaPickerController *) mediaPicker didPickMediaItems: (MPMediaItemCollection *) mediaItemCollection 
{
    [self dismissModalViewControllerAnimated: YES];
   appdelegate.selectedSongCollection=mediaItemCollection; 
    appdelegate.appMusicPlayer = [MPMusicPlayerController applicationMusicPlayer];

    [appdelegate.appMusicPlayer setQueueWithItemCollection:appdelegate.selectedSongCollection];
    [appdelegate.appMusicPlayer play];
    [btnStop setHidden:NO];
    [btnMusic setHidden:YES];
}

- (void) mediaPickerDidCancel: (MPMediaPickerController *) mediaPicker 
{   
    [self dismissModalViewControllerAnimated: YES]; 
}

有关如何在应用程序进入前景时恢复它的任何想法?

1 个答案:

答案 0 :(得分:2)

我会查看该曲目的currentPlaybackTime。 MPMusicPlayerController具有currentPlaybackTime属性。当应用程序进入后台时,只需保存此值(因为属性应该可以正常工作),并且在WillEnterForeground中,使用相同属性的值来设置恢复时的currentPlaybackTime。