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
AVAudioSessionModeSpokenAudioiOS9新增加的
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
iOS AVAudioSession 配置(录音完声音变小问题)的更多相关文章
- iOS AVAudioPlayer播放音频时声音太小
iOS AVAudioPlayer播放音频时声音太小 //引入AVFoundation类库,设置播放模式就可以了 do { try AVAudioSession.sharedInstance().ov ...
- iOS录音后播放声音变小的解决方法
目前需求是录音之后再播放出来.经常会出现播放声音变很小的情况. 解决方法: if (recorder.recording){ [recorder stop]; } [[AVAudioSession s ...
- mac QQ 语音或视频时其他声音变小的解决办法
在使用MacBook的时候,使用QQ视频的同时 看视频 听歌都会发现,QQ视频声音正常,其他软件的声音会很小很小.怎么解决呢,首先 开启QQ后,在视频之前: 1.打开终端输入以下命令. printf ...
- 移动端实现HTML5 mp3录音踩坑指南:系统播放音量变小、一些机型录音断断续续 之 MediaRecorder和AudioWorklet的终极对决
目录 H5录音见坑填坑 采用MediaRecorder采集音频 音频格式:WebM和PCM 从WebM封装容器中提取PCM数据 录音的兼容性 困扰已久的H5录音时系统播放音量变小的问题 H5录音见坑填 ...
- Android 4.3实现类似iOS在音乐播放过程中如果有来电则音乐声音渐小铃声渐大的效果
目前Android的实现是:有来电时,音乐声音直接停止,铃声直接直接使用设置的铃声音量进行铃声播放. Android 4.3实现类似iOS在音乐播放过程中如果有来电则音乐声音渐小铃声渐大的效果. 如果 ...
- [RK3288][Android6.0] 调试笔记 --- 录音音量从HAL到APP层会变小问题【转】
本文转载自:http://blog.csdn.net/kris_fei/article/details/72783843?locationNum=9&fps=1 Platform: Rockc ...
- 杜比(dolby)自动关闭,windows10声音自动变小
电脑问题描述:2018.01.21 win10更新后,看视频电脑声音自动变小,重开机电脑声音正常,一会又会变小.找了很多网上的东西,实践后发现是杜比(dolby)自动关闭导致的,自动关闭的原因是因为切 ...
- iOS开发之录音
录音 除了上面说的,在AVFoundation框架中还要一个AVAudioRecorder类专门处理录音操作,它同样支持多种音频格式.与AVAudioPlayer类似,你完全可以将它看成是一个录音机控 ...
- vue-cli3.0结合lib-flexible、px2rem实现移动端适配,完美解决第三方ui库样式变小问题
公司最近做的一个移动端项目从搭框架到前端开发由我独立完成,以前做移动端适配用的媒体查询,这次想用点别的适配方案,然后就采用了vue-cli3.0结合lib-flexible.px2rem实现移动端适配 ...
随机推荐
- (六)cxf处理一些Map等复杂类型
前面讲的一些都是简单类型,cxf都支持.但是有些复杂类型,cxf是不支持,比如常用的Map类型: 下面我们在前面的实例基础上在加一个方法,比如我们现在有个需求,获取所有用用户以及对应的每个用户所有角色 ...
- Java编程的逻辑 (12) - 函数调用的基本原理
本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http:/ ...
- async/await套路编程
对于并发任务,通常是用生成消费模型,对队列的处理可以使用类似master-worker的方式,master主要用户获取队列的msg,worker用户处理消息. 为了简单起见,并且协程更适合单线程的方式 ...
- AOJ 0009 Prime Number
题意:给出n,求不大于n的素数有多少个. 算法:先用线性时间复杂度的筛法打素数表,对于每个输入统计不超过的素数个数. #include <cstdio> int p[100010]; bo ...
- Scrapy项目结构分析和工作流程
新建的空Scrapy项目: spiders目录: 负责存放继承自scrapy的爬虫类.里面主要是用于分析response并提取返回的item或者是下一个URL信息,每个Spider负责处理特定的网站或 ...
- 类的 __call__ 和__repr__ 方法
__call__: 让类实例可以被调用: __str__ , __repr__ : 两个都能是类实例名能被打印,区别在于repr可在交互是直接打印类名不用加print
- laravel 控制器使用MODEL
第一步:引入MODEL类 use App\Http\Models\Sysdba; 第二步:使用 $uid = $request->input('uid'); 方法1. $model = new ...
- POJ 3304 Segments (叉乘判断线段相交)
<题目链接> 题目大意: 给出一些线段,判断是存在直线,使得该直线能够经过所有的线段.. 解题思路: 如果有存在这样的直线,过投影相交区域作直线的垂线,该垂线必定与每条线段相交,问题转化为 ...
- 计蒜客 淘宝的推荐系统【DP】
题目链接:https://nanti.jisuanke.com/t/26984 ...
- Flutter常用组件(Widget)解析-Image
显示图片的组件 以下是几种加载图片路径方式: Image.asset 加载asset项目资源中的文件 Image.network 加载网络资源图片,通过url加载 Image.file 加载本地文件中 ...