有这么一个场景,首先我们录音,录音完再播放发现音量变小了;
百思不得其解,查看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