2011-08-30 63 views
4

AVPlayer類中是否有任何委託方法?我需要處理諸如電話等AVAudioPlayer支持的中斷。如果AVPlayer不支持它,如何使用AVAudioPlayer播放音頻?AVPlayer類事件

回答

8

AVPlayer沒有你想要的方式,但你可以使用AVAudioSession對象,而不是

1)選擇AVAudioSession對象(例如[AVAudioSession sharedInstance]
2)設置激活它通過調用setActive:error:方法
3)集其委託(類實施AVAudioSessionDelegate協議)
4)執行委託的方法,如

-(void)beginInterruption; 
-(void)endInterruptionWithFlags:(NSUInteger)flags; 
-(void)endInterruption; 
1

編輯

我沒有看到AVPlayer class

因此,如何傳輸音頻與AVAudioPlayer任何可用的代表?因爲我們不知道你需要怎麼流呢,並從那裏最重要的,providind一些啓示
查看相關問題:

和教程


AVAudioPlayerDelegate協議參考http://developer.apple.com/library/ios/#documentation/AVFoundation/Reference/AVAudioPlayerDelegateProtocolReference/Reference/Reference.html#//apple_ref/doc/uid/TP40008068

  • 響應聲音播放完成
    - audioPlayerDidFinishPlaying:成功:

  • 應對方式的音頻解碼錯誤
    - audioPlayerDecodeErrorDidOccur:錯誤:

  • 處理音頻中斷
    - audioPlayerBeginInterruption :
    - audioPlayerEn dInterruption:
    - audioPlayerEndInterruption:withFlags:

+1

我知道這件事。我寫在我的問題:)「AVAudioPlayer支持它」,我對AVPlayer –

+0

感興趣,我錯過了那個問題的部分,編輯 –

1

我不認爲AVPlayer將讓你有。看看AVAudioPlayerDelegate,audioPlayerBeginInterruption將是您正在尋找的委託方法。

下面是一個代碼示例中,我使用AVAudioPlayer(我假設你已經知道如何建立你的網址):

// Instantiates the AVAudioPlayer object, initializing it with the sound 
NSError * errAV = nil; 
AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfUrl: mUrl error: &errAV]; 
if (newPlayer == nil) { 

    NSString * msg = [[NSString alloc] initWithFormat:@"An internal error has occured: %@", [errAV localizedDescription]]; 
    UIAlertView *uiav = [[UIAlertView alloc] initWithTitle:@"Play Sound" 
                message:msg delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [uiav show]; 
    [uiav release]; 
    [msg release]; 
} else { 

    self.appSoundPlayer = newPlayer; 
    [newPlayer release]; 
    // "Preparing to play" attaches to the audio hardware and ensures that playback 
    //  starts quickly when the user taps Play 
    [appSoundPlayer prepareToPlay]; 
    [appSoundPlayer setVolume: 1.0]; 
    [appSoundPlayer setDelegate: self]; 
    [appSoundPlayer play]; 
} 
+0

請再讀一次問題:) –

+0

對不起,錯過了那部分!不,我不認爲你可以用AVPlayer做。使用AVAudioPlayer進行流式傳輸非常簡單 - 我會在我的主要內容中插入一些代碼,我不認爲我可以在這裏將代碼放入我的評論中! –

+0

有時您必須使用AVPlayer,但是...例如,AVAudioPlayer無法播放人的iTunes資料庫中的音頻。 – samkass

1

即使使用AVAudioPlayer時,你可以在你初始化一個音頻會話,其中可以指定您將要執行的播放類型(或錄製內容),以及處理電話呼叫等中斷的回調。

看看AudioSessionInitialize()它是第三個參數,一個處理中斷的回調函數。在您的回調中,您可以處理中斷的開始和結束。

在使用AudioSession和依賴AVAudioPlayer回調函數之間,顯着的區別在於前者發生在較低級別,可能在調用後者的委託方法之前。因此,使用AudioSession回調,我認爲您有更好的控制權,但是您可能需要做更多的事情,具體取決於應用音頻設置的複雜程度。

1

發佈問題已經很久了。然而,對於完成的緣故,我想補充:AVPlayer 可以被用於通過添加TimeObserver處理中斷如下:

當初始化的AVPlayer:

AVPlayer *_aplayer; 
id _aplayerObserver; 

_aplayer = [[AVPlayer alloc] initWithURL:mediaURL]; 
_aplayerObserver = [_aplayer addPeriodicTimeObserverForInterval:CMTimeMake(1.0, 1.0) queue:NULL usingBlock:^(CMTime time) 
{ 
    if (((time.value/time.timescale) >= (_aplayer.currentItem.asset.duration.value/_aplayer.currentItem.asset.duration.timescale)) 
    { 
     // media file played to its end 
     // you can add here code that should run after the media file is completed, 
     // thus mimicing AVAudioPlayer's audioPlayerDidFinishPlaying event 
    } 
    else 
    { 
     if (_aplayer.rate == 0) 
      // audio player was interrupted 
    } 
} 

如果你選擇這個解決方案,請注意addPeriodicTimeObserverForInterval的文檔說明:

You must retain the returned value [i.e. _aplayerObserver] as long as you want the time observer to be invoked by the player. Each invocation of this method should be paired with a corresponding call to removeTimeObserver:.