日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

iOS AVAudioSession 配置(录音完声音变小问题)

發布時間:2025/3/20 编程问答 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS AVAudioSession 配置(录音完声音变小问题) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

有這么一個場景,首先我們錄音,錄音完再播放發現音量變小了;

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

    轉載于:https://www.cnblogs.com/Free-Thinker/p/9578130.html

    總結

    以上是生活随笔為你收集整理的iOS AVAudioSession 配置(录音完声音变小问题)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。