有這么一個場景,首先我們錄音,錄音完再播放發現音量變小了;
百思不得其解,查看API發現AVAudioSession里面有這么一個選項,
如果你的app涉及到了音視頻通話以及播放其他語音,那么當遇到聲音變小的時候,可以看看下面的配置。
AVAudioSessionCategoryOptionDuckOthers
蘋果文檔上說,如果把AVAduioSession配置成這樣,那么我們當前的場景外,其他播放的聲音將會會變小;
比如在用手機導航時播放音樂,那么當導航的聲音播放時,音樂的聲音就需要調小,來達到讓導航的語音不受影響;
在導航聲音播放完之后,我們需要讓音樂的聲音重新回到正常,那么可以重新配置來激活;
當前這個場景也可以使用兩個播放器,直接控制音量來達到;
如下代碼
//在我們的音視頻場景配置,指定其他聲音被強制變小 AVAudioSession *ses = [AVAudioSession sharedInstance]; [ses setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDuckOthers error:nil ]; //當我們的場景結束時,為了不影響其他場景播放聲音變小; AVAudioSession *ses = [AVAudioSession sharedInstance]; [ses setActive:NO error:nil]; [ses setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:nil ]; [ses 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
使用硬件解碼器處理音頻,該音頻會話使用期間,不能播放或錄音
-
AVAudioSessionCategoryMultiRoute
多種音頻輸入輸出,例如可以耳機、USB設備同時播放等
AVAudionSessionMode
-
AVAudioSessionModeDefault
默認的模式,適用於所有的場景
-
AVAudioSessionModeVoiceChat
適用類別 AVAudioSessionCategoryPlayAndRecord ,應用場景VoIP
-
AVAudioSessionModeGameChat
適用類別 AVAudioSessionCategoryPlayAndRecord ,應用場景游戲錄制,由GKVoiceChat自動設置,無需手動調用
-
AVAudioSessionModeVideoRecording
適用類別 AVAudioSessionCategoryPlayAndRecord,AVAudioSessionCategoryRecord 應用場景視頻錄制
-
AVAudioSessionModeMoviePlayback
適用類別 AVAudioSessionCategoryPlayBack 應用場景視頻播放
-
AVAudioSessionModeVideoChat
適用類別 AVAudioSessionCategoryPlayAndRecord ,應用場景視頻通話
-
AVAudioSessionModeMeasurement
適用類別AVAudioSessionCategoryPlayAndRecord,AVAudioSessionCategoryRecord,AVAudioSessionCategoryPlayback
-
AVAudioSessionModeSpokenAudio
iOS9新增加的
AVAudioSessionCategoryOptions
-
AVAudioSessionCategoryOptionMixWithOthers
適用於AVAudioSessionCategoryPlayAndRecord, AVAudioSessionCategoryPlayback, and AVAudioSessionCategoryMultiRoute, 用於可以和其他app進行混音
-
AVAudioSessionCategoryOptionDuckOthers
適用於AVAudioSessionCategoryAmbient, AVAudioSessionCategoryPlayAndRecord, AVAudioSessionCategoryPlayback, and AVAudioSessionCategoryMultiRoute, 用於壓低其他聲音播放的音量,使期音量變小
-
AVAudioSessionCategoryOptionAllowBluetooth
適用於AVAudioSessionCategoryRecord and AVAudioSessionCategoryPlayAndRecord, 用於是否支持藍牙設備耳機等
-
AVAudioSessionCategoryOptionDefaultToSpeaker
適用於AVAudioSessionCategoryPlayAndRecord ,用於將聲音從Speaker播放,外放,即免提
-
AVAudioSessionCategoryOptionInterruptSpokenAudioAndMixWithOthers
適用於AVAudioSessionCategoryPlayAndRecord, AVAudioSessionCategoryPlayback, and AVAudioSessionCategoryMultiRoute, iOS9 新增加的
-
AVAudioSessionCategoryOptionAllowBluetoothA2DP
適用於AVAudioSessionCategoryPlayAndRecord,藍牙和a2dp
-
AVAudioSessionCategoryOptionAllowAirPlay
適用於AVAudioSessionCategoryPlayAndRecord,airplay
AVAudioSessionCategoryOptionDuckOthers
在設置 CategoryPlayAndRecord 時,同時設置option為Duckothers 那么會壓低其他音量播放
解決辦法,重新設置。
This allows an application to set whether or not other active audio apps will be ducked when when your app's audio
session goes active. An example of this is the Nike app, which provides periodic updates to its user (it reduces the
volume of any music currently being played while it provides its status). This defaults to off. Note that the other
audio will be ducked for as long as the current session is active. You will need to deactivate your audio
session when you want full volume playback of the other audio.
參考:http://www.jianshu.com/p/3e0a399380df