码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2017-09-25 13:20:22      阅读:5003      评论:0      收藏:0      [点我收藏+]

标签:配置   volume   demo   and   where   文档   play   owa   any   

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

百思不得其解,查看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 配置(录音完声音变小问题)

标签:配置   volume   demo   and   where   文档   play   owa   any   

原文地址:http://www.cnblogs.com/cocoajin/p/7591068.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!