【问题标题】:AVAudioSession AVAudioSessionCategoryPlayAndRecord glitchAVAudioSession AVAudioSessionCategoryPlayAndRecord 故障
【发布时间】:2014-12-23 00:36:21
【问题描述】:

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

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

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

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

有没有办法消除这个故障?

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

当应用程序进入后台(无论是否停用音频会话)时,也会发生配置更改(和故障)。当从后台返回而不停用音频会话时,当AVCaptureSession 配置更改时,即当摄像头从正面切换到背面时,就会开始出现故障。在这种情况下,音频路由不会被触及,它只会在从后台返回时才会发生,而不会停用音频会话。路由更改的通知被触发两次。一次禁用麦克风,然后再次启用它。

请注意,通过下载 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;

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

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

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

然后更改类别:

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

尽管有选项标志,其他音频仍会停止播放。不会抛出任何错误。

【问题讨论】:

  • 我也有同样的问题。给信。有这方面的消息吗?
  • 对于您的最后一个示例,尽管有选项标志,但音频停止播放。如果您在设置类别 [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionMixWithOthers | AVAudioSessionCategoryOptionDefaultToSpeaker error:nil]; 之前停用音频会话 ([[AVAudioSession sharedInstance] setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];),音频将继续播放。并在 ([[AVAudioSession sharedInstance] setActive:YES error:nil];) 之后重新激活
  • 也有同样的问题,一直尝试使用PlayAndRecordstackoverflow.com/questions/35496563/…。还尝试通过在录制开始时将类别从 Ambient 更改为:stackoverflow.com/questions/35615707/…。如果您找到任何一种方法的解决方案,请告诉我
  • Apple 表示 PlayAndRecord 的音频故障没有解决方法:forums.developer.apple.com/message/74778#74778。所以我想唯一的选择是改变类别,但这也不起作用? stackoverflow.com/questions/35615707/…

标签: ios objective-c avfoundation avcapturesession avaudiosession


【解决方案1】:

正如@Cbas 在 cmets 中指出的那样,Apple 员工已确认从仅输出路由切换到输入+输出路由时存在故障,并且该问题没有解决方法,可能的解决方法是完全避免切换从仅输出到输入输出路由始终使用AVAudioSessionCategoryPlayAndRecord 类别,即使应用程序未记录也是如此。 另外,如果音频会话类别已经设置为 AVAudioSessionCategoryPlayAndRecord,请不要再次设置,否则会出现故障。

【讨论】:

  • 警告:使用 AVAudioSessionCategoryPlayAndRecord 会禁用 MPNowPlayingInfoCenter(在锁定屏幕上)
【解决方案2】:
[self.captureSession startRunning];
[[AVAudioSession sharedInstance] setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];
[[AVAudioSession sharedInstance] setActive:YES withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];

当 capureSession startRunning 时,停用当前 audioSession 并在后台恢复其他中断的音乐应用程序使用此选项AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation

使用当前的音频会话重置类别AVAudioSessionCategoryPlayAndRecord,然后再次激活。

我希望它对你有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-09
    • 1970-01-01
    • 2016-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多