iOS AVAudioSession 配置(录音完声音变小问题)
2017-09-25 11:26
477 查看
有这么一个场景,首先我们录音,录音完再播放发现音量变小了;
百思不得其解,查看API发现AVAudioSession里面有这么一个选项,
如果你的app涉及到了音视频通话以及播放其他语音,那么当遇到声音变小的时候,可以看看下面的配置。
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #4dbf56 }
span.s1 { }
AVAudioSessionCategoryOptionDuckOthers
苹果文档上说,如果把AVAduioSession配置成这样,那么我们当前的场景外,其他播放的声音将会会变小;
比如在用手机导航时播放音乐,那么当导航的声音播放时,音乐的声音就需要调小,来达到让导航的语音不受影响;
在导航声音播放完之后,我们需要让音乐的声音重新回到正常,那么可以重新配置来激活;
当前这个场景也可以使用两个播放器,直接控制音量来达到;
如下代码
一. 配置AVAudioSession接口
二. 关闭与激活AVAudioSession配置接口
三. 音频开发的一些配置选项
当前App的播放声音可以和其他app播放的声音共存,当锁屏或按静音时停止。
只能播放当前App的声音,其他app的声音会停止,当锁屏或按静音时停止。
只能播放当前App的声音,其他app的声音会停止,当锁屏或按静音时不会停止。
只能用于录音,其他app的声音会停止,当锁屏或按静音时不会停止
在录音的同时播放其他声音,当锁屏或按静音时不会停止
使用硬件解码器处理音频,该音频会话使用期间,不能播放或录音
多种音频输入输出,例如可以耳机、USB设备同时播放等
默认的模式,适用于所有的场景
适用类别 AVAudioSessionCategoryPlayAndRecord ,应用场景VoIP
适用类别 AVAudioSessionCategoryPlayAndRecord ,应用场景游戏录制,由GKVoiceChat自动设置,无需手动调用
适用类别 AVAudioSessionCategoryPlayAndRecord,AVAudioSessionCategoryRecord 应用场景视频录制
适用类别 AVAudioSessionCategoryPlayBack 应用场景视频播放
适用类别 AVAudioSessionCategoryPlayAndRecord ,应用场景视频通话
适用类别AVAudioSessionCategoryPlayAndRecord,AVAudioSessionCategoryRecord,AVAudioSessionCategoryPlayback
iOS9新增加的
适用于AVAudioSessionCategoryPlayAndRecord, AVAudioSessionCategoryPlayback, and AVAudioSessionCategoryMultiRoute, 用于可以和其他app进行混音
适用于AVAudioSessionCategoryAmbient, AVAudioSessionCategoryPlayAndRecord, AVAudioSessionCategoryPlayback, and AVAudioSessionCategoryMultiRoute, 用于压低其他声音播放的音量,使期音量变小
适用于AVAudioSessionCategoryRecord and AVAudioSessionCategoryPlayAndRecord, 用于是否支持蓝牙设备耳机等
适用于AVAudioSessionCategoryPlayAndRecord ,用于将声音从Speaker播放,外放,即免提
适用于AVAudioSessionCategoryPlayAndRecord, AVAudioSessionCategoryPlayback, and AVAudioSessionCategoryMultiRoute, iOS9 新增加的
适用于AVAudioSessionCategoryPlayAndRecord,蓝牙和a2dp
适用于AVAudioSessionCategoryPlayAndRecord,airplay
解决办法,重新设置。
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
百思不得其解,查看API发现AVAudioSession里面有这么一个选项,
如果你的app涉及到了音视频通话以及播放其他语音,那么当遇到声音变小的时候,可以看看下面的配置。
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #4dbf56 }
span.s1 { }
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
相关文章推荐
- ios 音频处理开发(一)iOS AudioSession中的category配置问题
- AVAudioRecorder录音和AVAudioPlayer播放声音小的问题!
- iOS AVAudioSession、AudioSession Category选择和声音通道听筒、扬声器切换
- AVAudioRecorder录音和AVAudioPlayer播放声音小的问题!
- iOS录音后播放声音小,AudioSessionInitialize failed,AudioQueueStart failed (-50)
- iOS开发之录音和播放问题AVAudioPlayer
- 关于iOS录音后播放声音小的问题
- IOS中录音后再播放声音太小问题解决
- IOS AVAudioPlay 播放本地音频没声音问题
- cocos2d-x 工程跨平台在Xcode8.1中ios10.1真机录音等私有配置 info的配置问题
- iOS 使用AudioRecorder录音后在使用AVPlayer播放不了声音的问题
- IOS中录音后再播放声音太小问题解决
- iphone ios AVAudioRecorder 录音后用avplayer播放声音小
- iOS AVAudioRecorder 录音频率、声道、位数配置 wav格式
- web.xml配置Session超时时间注意的单位问题
- php开发和调试环境的配置+出现 57% waiting for xdebug session的问题
- iOS开发那些事-Git在Xcode中的配置与使用常见问题总结
- iOS开发那些事-Git在Xcode中的配置与使用常见问题总结 推荐
- iOS开发那些事-Git在Xcode中的配置与使用常见问题总结
- 在后台有音乐播放的情况下用AVAudioRecorder进行录音问题的解决