天天看點

ios中audio session紀錄

Solo Ambient:獨立播放,其他音頻暫停,不可以在背景播放(即使利用其它技術使背景仍在運作,音頻依然暫停),開啟靜音鍵時,能使其靜音

Ambient:和其他程式的音頻可以同時混播,不可以在背景播放(即使利用其它技術使背景仍在運作,音頻依然暫停),開啟靜音鍵時,能使其靜音。

Playback:可能與其他音頻混合播放(預設是不能,但可以設定),能在背景播放,開啟靜音鍵時,不能使其靜音。

Record:錄音,開啟靜音鍵時,不能使其靜音,能在背景錄音

Play and Record:表示音頻輸入和輸出,按順序或同時,可能與其他音頻混合播放(預設是不能,但可以設定),能在背景工作,開啟靜音鍵時,不能使其靜音

Audio Processing:應用程式執行硬體輔助音頻編碼(它不播放或記錄),開啟靜音鍵時,能否靜音未知,不能混合其他音頻,能在背景工作

例如,設定音頻能背景播放的session代碼:

AVAudioSession *session = [AVAudioSession sharedInstance];
    //這裡是設定當程式退出或者鎖屏時音頻怎麼處理,此設定是獨占背景播放模式,如果程式進入背景,除非有電話進來,否則音頻一直播放至完成,即使有其他音頻播放
    //AVAudioSessionCategoryOptionDuckOthers--和其他音頻能混合播放,但是目前app聲音最大  AVAudioSessionCategoryOptionMixWithOthers--
    //和其他音頻混合播放,聲音大小都一樣
 [session setCategory:AVAudioSessionCategoryPlayback
              withOptions:AVAudioSessionCategoryOptionDuckOthers
                    error:nil];
    NSError *error=nil;
[session setActive:YES  error:&error];
//當音頻處理完畢後,暫停服務,通知其他軟體恢複音頻
[[AVAudioSession  sharedInstance] setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];
           

當音頻被中斷後,在中斷源處理完畢後,可以接收到一個通知,然後可以相應恢複音頻

代碼示例:

//監聽音頻中斷
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioInterruption:) 
          name:AVAudioSessionInterruptionNotification object:nil];
           
-(void) audioInterruption:(NSNotification *)notification{
    NSNumber *interruptionType = [[notification userInfo] objectForKey:AVAudioSessionInterruptionTypeKey];
    NSNumber *interruptionOption = [[notification userInfo] objectForKey:AVAudioSessionInterruptionOptionKey];
    
    switch (interruptionType.unsignedIntegerValue) {
        case AVAudioSessionInterruptionTypeBegan:{
            // • Audio has stopped, already inactive
            // • Change state of UI, etc., to reflect non-playing state
        } break;
        case AVAudioSessionInterruptionTypeEnded:{
            NSError *error=nil;
            AVAudioSession *session = [AVAudioSession sharedInstance];
           //BOOL flag= session.secondaryAudioShouldBeSilencedHint;
           [session setActive:YES error:&error];
            if(error)
            {
                ADTLog(@"%@",error);

            }
            // • Make session active
            // • Update user interface
            // • AVAudioSessionInterruptionOptionShouldResume option
            if (interruptionOption.unsignedIntegerValue == AVAudioSessionInterruptionOptionShouldResume) {
           
                if (_TTSsynthesizer != nil) {
                    [_TTSsynthesizer continueSpeaking];
                    _playStatus = 1;
                }
        
                else if ((_audioPlayer != nil)) {
                    [_audioPlayer prepareToPlay];
                    [_audioPlayer play];
                    _playStatus = 3;
                }
              
                else if (_audioStreamer != nil) {
                    _audioStreamer.state=AS_PAUSED;
                    [_audioStreamer start];
                    _playStatus = 5;
                }
            }
        } break;
        default:
            break;
    }
}
           

繼續閱讀