2013-03-19 28 views
5

我正在使用AVPlayer播放音頻文件。我實施了AVAudioSessionInterruptionNotification如何獲取AVPlayer的中斷開始/結束事件

AVAudioSession *session = [AVAudioSession sharedInstance]; 
    NSError *errorInAudio = nil; 
    [session setActive:YES error:&errorInAudio]; 

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(interruption:) name:AVAudioSessionInterruptionNotification object:nil]; 

    [session setCategory:AVAudioSessionCategoryPlayback error:nil]; 

當應用程序在前臺(如語音控制)時發生中斷時,它可以正常工作。
但我在後臺模式下創建了應用程序並打開了iPod播放器並開始播放。當時甚至當我的應用程序進入前臺時,它的中斷不會被調用。
可能是什麼問題。請幫忙。

回答

3

如果使用AVAudioPlayer可以符合AVAudioPlayerDelegate那麼你就可以實現像docs上市- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag

多個方法的方法。

+0

感謝您的答覆。我得到了AVAudioPlayer的代表。但我想知道是否有任何方法使用AVPlayer來實現這一點。你能提出一個想法嗎? – 2013-03-19 15:41:36

+0

我對AVPlayer沒有太多的經驗,但是這個SO問題涵蓋了同樣的問題:http://stackoverflow.com/questions/6837002/no-avplayer-delegate-how-to-track-when-song-finished -playing-objective-c-iphon – 2013-03-19 15:54:12

+0

無論如何,我正在將AVPlayer更改爲AVAudioPlayer。謝謝你的建議。 – 2013-03-19 18:16:58

1

得到通知時AVPlayer在後臺被中斷,你可以聽的AudioSession的通知

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerStopped) name:AVAudioSessionInterruptionNotification object:nil]; 
相關問題