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

  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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS 上,你可以使用 AVAudioRecorder 和 AVAudioPlayer 来同时进行录音和播放。 首先,你需要创建一个 AVAudioRecorder 实例来录制音频。然后,你可以在录制音频的同时,创建一个 AVAudioPlayer 实例来播放音频。在录制和播放过程中,你需要将音频数据写入文件和缓存中。 以下是一个示例代码,展示了如何同时录制和播放音频: ```swift import AVFoundation class AudioController: NSObject, AVAudioRecorderDelegate { var recorder: AVAudioRecorder? var player: AVAudioPlayer? func startRecordingAndPlayback() { // 设置录音会话 let session = AVAudioSession.sharedInstance() try? session.setCategory(AVAudioSession.Category.playAndRecord, mode: .default, options: []) try? session.setActive(true) // 设置录音器 let settings: [String: Any] = [AVFormatIDKey: Int(kAudioFormatMPEG4AAC), AVSampleRateKey: 44100.0, AVNumberOfChannelsKey: 2, AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue] let url = URL(fileURLWithPath: NSTemporaryDirectory() + "recording.m4a") recorder = try? AVAudioRecorder(url: url, settings: settings) recorder?.delegate = self recorder?.prepareToRecord() recorder?.record() // 设置播放器 let audioData = try? Data(contentsOf: url) player = try? AVAudioPlayer(data: audioData ?? Data()) player?.prepareToPlay() player?.play() } // AVAudioRecorderDelegate 方法 func audioRecorderDidFinishRecording(_ recorder: AVAudioRecorder, successfully flag: Bool) { player?.stop() } } ``` 在这个示例中,我们首先设置了一个音频会话,然后创建了一个 AVAudioRecorder 实例来录制音频。同时,我们也创建了一个 AVAudioPlayer 实例来播放音频。最后,在录音成后,我们停止了播放器。 你可以根据自己的需求进行更改和调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值