AVAudioSession AVAudioSessionCategoryPlayAndRecord故障 [英] AVAudioSession AVAudioSessionCategoryPlayAndRecord glitch

查看:444
本文介绍了AVAudioSession AVAudioSessionCategoryPlayAndRecord故障的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 AVCaptureSession 录制带有音频的视频。为此,我需要AudioSessionCategory AVAudioSessionCategoryPlayAndRecord ,因为我的应用程序还播放带声音的视频。

I would like to record videos with audio using AVCaptureSession. For this I need the AudioSessionCategory AVAudioSessionCategoryPlayAndRecord, since my app also plays back video with sound.

我希望从默认扬声器中听到音频,我希望它能与其他音频混音。所以我需要选项 AVAudioSessionCategoryOptionDefaultToSpeaker | AVAudioSessionCategoryOptionMixWithOthers

I want audio to be audible from the default speaker and I want it to mix with other audio. So I need the options AVAudioSessionCategoryOptionDefaultToSpeaker | AVAudioSessionCategoryOptionMixWithOthers.

如果我在播放其他音频时执行以下操作,则其他应用的音频中会出现明显的可听故障:

If I do the following while other audio is playing there is a clear audible glitch in the audio from the other app:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionMixWithOthers | AVAudioSessionCategoryOptionDefaultToSpeaker error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];

有没有办法摆脱这个小故障?

Is there a way to get rid of this glitch?

当没有麦克风输入,从 AVAudioSessionRouteChangeReasonCategoryChange 通知 AVAudioSessionRouteChangeReasonRouteConfigurationChange 时,似乎会发生麦克风输入。

It seems to occur when the there's a AVAudioSessionRouteChangeReasonRouteConfigurationChange as notified by AVAudioSessionRouteChangeReasonCategoryChange from no mic input, to mic input.

当应用程序进入后台时(无论是否取消激活音频会话),也会发生配置更改(和故障)。当从后台返回而不停用音频会话时,当 AVCaptureSession 配置改变时,即当摄像机从前向后切换时,故障开始发生。在这种情况下,不触摸音频路由,并且仅在从后台返回而不停用音频会话时发生。路线更改的通知被触发两次。一旦禁用麦克风,一次再启用它。

The configuration change (and glitch) also occurs when the app goes to the background (with or without deactivating the audio session). When returning from the background without deactivating the audio session, the glitches start to occur when the AVCaptureSession configuration is changed i.e. when the camera is switched from front to back. In this case the audio routing is not touched, and it only occurs when returning from the background without deactivating the audio session. The notification that the route changes is fired twice. Once to disable the mic, and once to enable it again.

请注意,通过下载Apple的AVCamManual示例,可以轻松重现此行为。将以下内容添加到 viewDidLoad AAPLCameraViewController.m

Note that this behavior is easily reproducible by downloading Apple's AVCamManual example. Add the following to the viewDidLoad of AAPLCameraViewController.m:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionMixWithOthers | AVAudioSessionCategoryOptionDefaultToSpeaker error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];
…
session.usesApplicationAudioSession = YES;
session.automaticallyConfiguresApplicationAudioSession = NO;

其他一些奇怪的事情,可能是相关的:

Some other weird thing, which might be related:

首先将音频类别设置为 AVAudioSessionCategoryAmbient 并激活它:

Set the audio category to AVAudioSessionCategoryAmbient first and activate it:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient withOptions:0 error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];

然后更改类别:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionMixWithOthers | AVAudioSessionCategoryOptionDefaultToSpeaker error:nil];

尽管有选项标记,但其他音频停止播放。没有错误被抛出。

other audio stops playing despite the options flag. No errors are thrown.

推荐答案

正如@Cbas在评论中指出的那样,Apple员工确认切换时存在故障从仅输出到输入+输出路由,并且没有针对该问题的解决方法,可能的解决方法是始终使用 AVAudioSessionCategoryPlayAndRecord 类别。
另外,如果音频会话类别已经设置为AVAudioSessionCategoryPlayAndRecord或者发生故障,请不要再次设置音频会话类别。

As pointed out by @Cbas in the comments, an Apple Staff has confirmed there is glitch when switching from output-only to input+output routes and that there is no workaround for that issue, a possible workaround is to totally avoid switching from output-only to input-output routes by always use the AVAudioSessionCategoryPlayAndRecord category even when the app is not recording. Also, don't set the audio session category again if it has already be set to AVAudioSessionCategoryPlayAndRecord or the glitch will occur.

这篇关于AVAudioSession AVAudioSessionCategoryPlayAndRecord故障的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆