AVAudioSession AVAudioSessionCategoryPlayAndRecord毛刺

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

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

如果我在播放其他audio时进行以下操作,则会从另一个应用程序的audio中看到清晰的声音故障:

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

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

AVAudioSessionRouteChangeReasonCategoryChange从没有话筒input到话筒input时, AVAudioSessionRouteChangeReasonRouteConfigurationChange通知AVAudioSessionRouteChangeReasonCategoryChange

应用程序转到后台时(也可以不closuresaudio会话)也会发生configuration更改(和毛刺)。 在不closuresaudio会话的情况下从后台返回时,当AVCaptureSessionconfiguration发生变化,即相机从前向后切换时,会出现毛刺。 在这种情况下,audio路由不被触摸,并且仅在从后台返回而不closuresaudio会话时发生。 路由改变的通知被激发两次。 一次禁用麦克风,一次再次启用。

请注意,通过下载Apple的AVCamManual示例,可以很容易地重现此行为。 将以下内容添加到AAPLCameraViewController.mviewDidLoad中:

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

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

首先将audio类别设置为AVAudioSessionCategoryAmbient并将其激活:

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

然后更改类别:

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

其他audio停止播放尽pipe选项标志。 没有错误被抛出。