音頻:AVAudioSession 配置

遇到這么個場景,項目中有些頁面是來自cocos2d,其中涉及到音頻播放和錄制(應該對應AVAudioSessionCategoryPlayAndRecord);然后OC部分也涉及到音視頻的播放。存在從cocos2d場景退出后再播放OC場景下的視頻存在音頻聲音小,應該是處于聽筒模式。

對于上面的問題一般都想到AVAudioSession(主要用來管理音頻設置與硬件交互)單利設置系統(tǒng)使用音頻的方式:

/// 在我們的音視頻場景配置,指定其他聲音被強制變小

AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayAndRecord
             withOptions:AVAudioSessionCategoryOptionDuckOthers
                   error:nil ];

/// 當我們的場景結束時,為了不影響其他場景播放聲音變小;

AVAudioSession *session = [AVAudioSession sharedInstance];
[session setActive:NO error:nil];
[session setCategory:AVAudioSessionCategoryPlayAndRecord
             withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker
                   error:nil ];
[session setActive:YES error:nil];

一、 配置AVAudioSession接口

/* set session category */
- (BOOL)setCategory:(NSString *)category error:(NSError **)outError;
/* set session category with options */
- (BOOL)setCategory:(NSString *)category withOptions:(AVAudioSessionCategoryOptions)options error:(NSError **)outError NS_AVAILABLE_IOS(6_0);
/* set session category and mode with options */
- (BOOL)setCategory:(NSString *)category mode:(NSString *)mode options:(AVAudioSessionCategoryOptions)options error:(NSError **)outError NS_AVAILABLE_IOS(10_0);

二、關閉與激活AVAudioSession配置接口

/* Set the session active or inactive. Note that activating an audio session is a synchronous (blocking) operation.
 Therefore, we recommend that applications not activate their session from a thread where a long blocking operation will be problematic.
 Note that this method will throw an exception in apps linked on or after iOS 8 if the session is set inactive while it has running or 
 paused I/O (e.g. audio queues, players, recorders, converters, remote I/Os, etc.).
*/
- (BOOL)setActive:(BOOL)active error:(NSError **)outError;
- (BOOL)setActive:(BOOL)active withOptions:(AVAudioSessionSetActiveOptions)options error:(NSError **)outError NS_AVAILABLE_IOS(6_0);

三、AVAudioSessionCategory配置項

AVAudioSessionCategoryAmbient
當前App的播放聲音可以和其他app播放的聲音共存,當鎖屏或按靜音時停止。

AVAudioSessionCategorySoloAmbient
只能播放當前App的聲音,其他app的聲音會停止,當鎖屏或按靜音時停止。

AVAudioSessionCategoryPlayback
只能播放當前App的聲音,其他app的聲音會停止,當鎖屏或按靜音時不會停止。

AVAudioSessionCategoryRecord
只能用于錄音,其他app的聲音會停止,當鎖屏或按靜音時不會停止

AVAudioSessionCategoryPlayAndRecord
在錄音的同時播放其他聲音,當鎖屏或按靜音時不會停止

AVAudioSessionCategoryAudioProcessing
使用硬件解碼器處理音頻,該音頻會話使用期間,不能播放或錄音(iOS10棄用)

AVAudioSessionCategoryMultiRoute
多種音頻輸入輸出,例如可以耳機、USB設備同時播放等

四、AVAudionSessionMode配置項:

AVAudioSessionModeDefault
默認的模式,適用于所有的場景

AVAudioSessionModeVoiceChat
適用類別 AVAudioSessionCategoryPlayAndRecord ,應用場景VoIP

AVAudioSessionModeGameChat
適用類別 AVAudioSessionCategoryPlayAndRecord ,應用場景游戲錄制,由GKVoiceChat自動設置,無需手動調用

AVAudioSessionModeVideoRecording
適用類別 AVAudioSessionCategoryPlayAndRecord,AVAudioSessionCategoryRecord 應用場景視頻錄制

AVAudioSessionModeMoviePlayback
適用類別 AVAudioSessionCategoryPlayBack 應用場景視頻播放

AVAudioSessionModeVideoChat
適用類別 AVAudioSessionCategoryPlayAndRecord ,應用場景視頻通話

AVAudioSessionModeMeasurement
適用類別AVAudioSessionCategoryPlayAndRecord,AVAudioSessionCategoryRecord,AVAudioSessionCategoryPlayback

AVAudioSessionModeSpokenAudio
iOS9新增加的。如果另一個應用程序播放語音提示,您的應用程序應該暫停而不是躲避它的音頻。在中斷的應用程序的音頻結束后,您可以繼續(xù)您的應用程序的音頻播放。

五、AVAudioSessionCategoryOptions配置項

AVAudioSessionCategoryOptionMixWithOthers
適用于:
AVAudioSessionCategoryPlayAndRecord
AVAudioSessionCategoryPlayback
AVAudioSessionCategoryMultiRoute
用于可以和其他app進行混音

AVAudioSessionCategoryOptionDuckOthers
適用于AVAudioSessionCategoryAmbient, AVAudioSessionCategoryPlayAndRecord, AVAudioSessionCategoryPlayback, and AVAudioSessionCategoryMultiRoute, 用于壓低其他聲音播放的音量,使期音量變小;在設置 CategoryPlayAndRecord 時,同時設置option為Duckothers 那么會壓低其他音量播放

AVAudioSessionCategoryOptionAllowBluetooth
適用于AVAudioSessionCategoryRecord ,AVAudioSessionCategoryPlayAndRecord, 用于是否支持藍牙設備耳機等

AVAudioSessionCategoryOptionDefaultToSpeaker
適用于AVAudioSessionCategoryPlayAndRecord ,用于將聲音從Speaker播放,外放,即免提

AVAudioSessionCategoryOptionInterruptSpokenAudioAndMixWithOthers
適用于AVAudioSessionCategoryPlayAndRecord, AVAudioSessionCategoryPlayback, and AVAudioSessionCategoryMultiRoute, iOS9 新增加的

AVAudioSessionCategoryOptionAllowBluetoothA2DP
適用于AVAudioSessionCategoryPlayAndRecord,藍牙和a2dp

AVAudioSessionCategoryOptionAllowAirPlay
適用于AVAudioSessionCategoryPlayAndRecord,airplay

六、監(jiān)聽音頻通知

AVAudioSessionRouteChangeNotification
/// 監(jiān)聽外部設備改變

 [[NSNotificationCenter defaultCenter] addObserver:self
                                              selector:@selector(audioRouteChangeListenerCallback:)
                                                  name:AVAudioSessionRouteChangeNotification
                                                object:nil];

AVAudioSessionSilenceSecondaryAudioHintNotification
/// 其他app占用音頻通道

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(otherAppAudioSessionCallBack:)
                                                 name:AVAudioSessionSilenceSecondaryAudioHintNotification
                                               object:nil];

AVAudioSessionInterruptionNotification
/// 電話、鬧鈴等一般性中斷通知

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(systermAudioSessionCallBack:)
                                                 name:AVAudioSessionInterruptionNotification
                                               object:nil];
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內容