2011-01-12 48 views
6

我有MPMoviePlayerController的問題。我用一個實例來發揮M3U8音源:MPMoviePlayerController/AVAudioSession在後臺不會在來電後重新開始播放

AVAudioSession *audioSession = [AVAudioSession sharedInstance]; 

NSError *setCategoryError = nil; 
[audioSession setCategory:AVAudioSessionCategoryPlayback error:&setCategoryError]; 
if (setCategoryError) { 
} 

NSError *activationError = nil; 
[audioSession setActive:YES error:&activationError]; 
if (activationError) { 
} 
self.player = 
[[MPMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString:url]]; 
player.view.hidden = YES; 
player.shouldAutoplay = YES; 
[player release]; 
[btnContainer addSubview: player.view]; 
player.useApplicationAudioSession = NO; 

它的設計,當應用程序被切換到後臺,一切工作正常發揮。

問題是當它在後臺,我得到一個來電。在這種情況下,流暫停,但通話結束後不會返回。事實上,控制檯說

2011-01-12 12:02:27.729 RAC1[1571:307] MP _playbackInterruptionDidEndNotification :: NSConcreteNotification 0x155890 {name = AVController_PlaybackInterruptionDidEndNotification; object = <AVController: 0x180d50>; userInfo = { 
    "AVController_InterruptionStatusNotificationParameter" = "call.declined"; 
    "AVController_InterruptorNameNotificationParameter" = Phone; 
}}, _state = 6 
2011-01-12 12:02:27.730 RAC1[1571:307] MP _playbackInterruptionDidEndNotification :: resuming playback! 

和應用程序確實顯示了流作爲MPMoviePlaybackStatePlaying,但聲音似乎停止。我曾試過

[[AVAudioSession sharedInstance] setActive: YES error: &err] 

但它似乎失敗了。

有沒有人有線索?

謝謝!

回答

3

好像我需要

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; 

在我的viewController的viewDidAppear ....

+0

當按下播放按鈕時,我開始接收遙控事件,並在按下停止按鈕時結束接收控制事件。另外我需要等待1秒鐘才能繼續播放,當應用程序在後臺播放音頻時。使用:dispatch_after(dispatch_time(DISPATCH_TIME_NOW,1 * NSEC_PER_SEC),dispatch_get_main_queue(),^ { // Resume Playing }) – 2012-03-15 21:33:16

1

難道你看this link處理音頻中斷。您需要設置AVAudioSessionDelegate並處理中斷。