2012-04-08 98 views
2

我已經實現了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]; 
} 

如何恢復它時,應用程序進入前景任何想法?

+0

我會在WillEnterForeground中檢查您嘗試播放的歌曲的查找時間。我認爲它正在重置,所以只需將它保存在didEnterBackground並將其設置在WillEnterForeground – jmstone617 2012-04-09 00:00:34

回答

2

我會查看曲目的當前播放時間。 MPMusicPlayerController具有currentPlaybackTime屬性。當應用程序要進入後臺時,只需保存該值(因爲屬性應該可以正常工作);在WillEnterForeground中,恢復時使用相同的屬性值設置currentPlaybackTime。

+0

好主意,將嘗試並恢復。我昨天最終做的是使用iPodMusicPlayer代替applicationMusicPlayer,並分別在後臺和前臺方法中暫停和恢復它。 – 2012-04-09 05:27:49

+0

我曾希望它不會出現這種情況,但歸功於'NSNotificationCenter',它仍然是一個可行的解決方案。 – Eugene 2012-11-15 19:57:27

相關問題