简体   繁体   English

AVAudioSession setCategory .allowBluetooth 导致崩溃

[英]AVAudioSession setCategory .allowBluetooth causes crash

I'm writing an app that records user audio with AVAudioSession.我正在编写一个使用 AVAudioSession 记录用户音频的应用程序。 All works well when I'm not adding bluetooth to the options, but I'd like to be able to record with AirPods as well.当我没有在选项中添加蓝牙时,一切正常,但我也希望能够使用 AirPods 进行录音。 When I add the .allowBluetooth option, it produces a crash and no longer works.当我添加 .allowBluetooth 选项时,它会导致崩溃并且不再起作用。

        do {
            let session = AVAudioSession.sharedInstance()
            try session.setCategory(.record, mode: .default, options: [.defaultToSpeaker, .allowBluetooth])
            try session.setActive(true)
        } catch let error as NSError {
            print(error.localizedDescription)
            return
        }

Any suggestions on this?对此有何建议? I've looked through numerous SO posts related to the subject and found nothing that seems to solve my issue.我浏览了许多与该主题相关的 SO 帖子,但没有发现似乎可以解决我的问题。

You are getting error code -50, which indicates invalid parameters.您收到错误代码 -50,表示参数无效。

This is because the .defaultToSpeaker option can only be used with the playAndRecord category:这是因为.defaultToSpeaker选项只能与playAndRecord类别一起使用:

You can set this option can only when using the playAndRecord category.只有在使用playAndRecord类别时才能设置此选项。 It's used to modify the category's routing behavior so that audio is always routed to the speaker rather than the receiver if no other accessories, such as headphones, are in use.它用于修改类别的路由行为,以便在没有使用其他附件(例如耳机)时,音频始终路由到扬声器而不是接收器。

So either remove this option or use a playAndRecord category.因此,要么删除此选项,要么使用playAndRecord类别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM