我想使用录制带有音频的视频AVCaptureSession
。为此,我需要 AudioSessionCategoryAVAudioSessionCategoryPlayAndRecord
,因为我的应用程序还播放有声视频。
我希望从默认扬声器中听到音频,并且希望它与其他音频混合。所以我需要选项AVAudioSessionCategoryOptionDefaultToSpeaker | AVAudioSessionCategoryOptionMixWithOthers
.
如果我在播放其他音频时执行以下操作,则其他应用程序的音频中会出现明显的可听故障:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionMixWithOthers | AVAudioSessionCategoryOptionDefaultToSpeaker error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];
有没有办法消除这个故障?
它似乎发生在有一个AVAudioSessionRouteChangeReasonRouteConfigurationChange
据通知AVAudioSessionRouteChangeReasonCategoryChange
从无麦克风输入到麦克风输入。
当应用程序进入后台(无论是否停用音频会话)时,也会发生配置更改(和故障)。当从后台返回而不停用音频会话时,当AVCaptureSession
配置发生变化,即当相机从前切换到后时。在这种情况下,不会触及音频路由,并且仅在从后台返回而不停用音频会话时才会发生。路线更改的通知会被触发两次。一次禁用麦克风,一次再次启用它。
请注意,通过下载 Apple 的 AVCamManual 示例,可以轻松重现此行为。将以下内容添加到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;
其他一些可能相关的奇怪的事情:
将音频类别设置为AVAudioSessionCategoryAmbient
首先并激活它:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient withOptions:0 error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];
然后更改类别:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionMixWithOthers | AVAudioSessionCategoryOptionDefaultToSpeaker error:nil];
尽管有选项标志,其他音频仍停止播放。不会抛出任何错误。