iOS AVAudioSession 配置(錄音完聲音變小問題)

來源:互聯網
上載者:User

標籤:配置   volume   demo   and   where   文檔   play   owa   any   

有這麼一個情境,首先我們錄音,錄音完再播放發現音量變小了;

百思不得其解,查看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
  1. AVAudioSessionCategoryAmbient

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

  2. AVAudioSessionCategorySoloAmbient

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

  3. AVAudioSessionCategoryPlayback

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

  4. AVAudioSessionCategoryRecord

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

  5. AVAudioSessionCategoryPlayAndRecord

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

  6. AVAudioSessionCategoryAudioProcessing

    使用硬體解碼器處理音頻,該音頻會話使用期間,不能播放或錄音

  7. AVAudioSessionCategoryMultiRoute

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

AVAudionSessionMode
  1. AVAudioSessionModeDefault

    預設的模式,適用於所有的情境

  2. AVAudioSessionModeVoiceChat

    適用類別 AVAudioSessionCategoryPlayAndRecord ,應用情境VoIP

  3. AVAudioSessionModeGameChat

    適用類別 AVAudioSessionCategoryPlayAndRecord ,應用情境遊戲錄製,由GKVoiceChat自動化佈建,無需手動調用

  4. AVAudioSessionModeVideoRecording

    適用類別 AVAudioSessionCategoryPlayAndRecord,AVAudioSessionCategoryRecord 應用情境視頻錄製

  5. AVAudioSessionModeMoviePlayback

    適用類別 AVAudioSessionCategoryPlayBack 應用情境視頻播放

  6. AVAudioSessionModeVideoChat

    適用類別 AVAudioSessionCategoryPlayAndRecord ,應用情境視訊通話

  7. AVAudioSessionModeMeasurement

    適用類別AVAudioSessionCategoryPlayAndRecord,AVAudioSessionCategoryRecord,AVAudioSessionCategoryPlayback

  8. AVAudioSessionModeSpokenAudio

    iOS9新增加的

AVAudioSessionCategoryOptions
  1. AVAudioSessionCategoryOptionMixWithOthers

    適用於AVAudioSessionCategoryPlayAndRecord, AVAudioSessionCategoryPlayback, and AVAudioSessionCategoryMultiRoute, 用於可以和其他app進行混音

  2. AVAudioSessionCategoryOptionDuckOthers

    適用於AVAudioSessionCategoryAmbient, AVAudioSessionCategoryPlayAndRecord, AVAudioSessionCategoryPlayback, and AVAudioSessionCategoryMultiRoute, 用於壓低其他聲音播放的音量,使期音量變小

  3. AVAudioSessionCategoryOptionAllowBluetooth

    適用於AVAudioSessionCategoryRecord and AVAudioSessionCategoryPlayAndRecord, 用於是否支援藍牙裝置耳機等

  4. AVAudioSessionCategoryOptionDefaultToSpeaker

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

  5. AVAudioSessionCategoryOptionInterruptSpokenAudioAndMixWithOthers

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

  6. AVAudioSessionCategoryOptionAllowBluetoothA2DP

    適用於AVAudioSessionCategoryPlayAndRecord,藍芽和a2dp

  7. 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

iOS AVAudioSession 配置(錄音完聲音變小問題)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.