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

摘要:
有这样一幕。首先,我们录制声音,然后再次播放。我们发现体积减小;我忍不住看了一下API,发现AVAudioSession中有这样一个选项。如果您的应用程序涉及音频和视频通话以及播放其他语音,当语音变小时,您可以查看以下配置。AVAudioSessionCategorySoloAmbient只能播放当前应用程序的声音。其他应用程序的声音将停止,当屏幕锁定或按下静音按钮时,声音将停止。

有这么一个场景,首先我们录音,录音完再播放发现音量变小了;

百思不得其解,查看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 配置(录音完声音变小问题)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇linux_ext4恢复超级块.txtPaper Review: 基于单细胞转录组数据推断基因调控网络的方法比较下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

7步学会在Windows下上架iOS APP流程

之前用跨平台开发工具做了一个应用,平台可以同时生成安卓版和苹果版,想着也把这应用上架到App Store试试,于是找同学借了个苹果开发者账号,但没那么简单,还要用到Mac电脑的钥匙串申请发布证书和上传ipa,可没有Mac,同学的大老远的也不方便拿过来用,然后捣鼓了个虚拟机,卡的我怀疑人生,后面找到了一个工具,在Windows环境下终于上传成功,把我的iO...

11- APP性能测试GT工具的使用

对性能测试来说有服务端的性能与客户端(APP)的性能。   GT简介 1.GT(随身调)是APP的随身调测平台,它是直接运行在手机上的“集成调试环境”(IDTE) 2.利用GT,仅凭一部手机,无需连接电脑,即可对APP进行快速的性能测试(CPU,内存,流量,电量,帧率,路程度等等) 开发日志的查看,crash日志查看,网络数据包的抓取,APP内部参数的调试...

FastApi 进阶

前言 终于有了第一个使用 FastApi 编写的线上服务, 在开发的过程中还是遇到了些问题, 这里记录一下 正文 目录结构 我们知道, FastApi 的启动方式推荐使用 uvicorn, 其启动方式大致为 uvicorn main:app, 实际上 main 为该文件的名字, app 为生成的 FastApi 对象, 那么, 对于一个比较大的项目, 我们...

uniapp微信APP支付踩坑指南:报错errMsg: "requestPayment:fail errors"

  起因:uni-app微信app支付一直报错“errMsg“: “requestPayment:fail errors“,但是坑的是没有具体的原因,微信APP支付官方文档也很坑。我们看看有多坑:   返回结果-1,包含所有情况,至于什么情况,您自己猜去吧,唉~~   网上查了很多别人也是很多遇到这个问题,但是所说解决方案都无用,当然我们记录一下,也未尝...

IOS学习之路二十(程序json转换数据的中文字符问题解决)

ios请求web中的json数据的时候经常出现乱码问题: 例如请求结果可能如下:"U00e5U00a5U00bdU00e8U00aeU00a4"  在网上查到的解决方法是: 解析数据的时候,可以先把数据存放在NSdata对象中,再进行转码,例如 NSData*jsondata = [requestresponseData]; NSString*jsonS...

iOS 三种录制视频方式

随着每一代 iPhone 处理能力和相机硬件配置的提高,使用它来捕获视频也变得更加有意思。它们小巧,轻便,低调,而且与专业摄像机之间的差距已经变得非常小,小到在某些情况下,iPhone 可以真正替代它们。 这篇文章讨论了关于如何配置视频捕获管线 (pipeline) 和最大限度地利用硬件性能的一些不同选择。 这里有个使用了不同管线的样例 app,可以在Gi...