繁体   English   中英

无法为AVAudioSession设置类别

[英]Not able to set category for AVAudioSession

尝试使用AVAudioSession录制音频。 但是不能设置类别最终会导致模棱两可而没有上下文错误。

我正在使用Swift 4。

@objc func setupRecorder() {
    recordingSession = AVAudioSession.sharedInstance()

    do {
        try recordingSession?.setCategory(.playAndRecord, mode: .default)
        try recordingSession.setActive(true)
        recordingSession.requestRecordPermission() { [unowned self] allowed in
            DispatchQueue.main.async {
                if allowed {
                    self.startRecording()
                } else {
                    // failed to record!
                }
            }
        }
    } catch {
        // failed to record!
    }
}

在此处输入图片说明

如果使用“ Swift4”,则设置类别的方法不同,即

AVAudioSession.sharedInstance().setCategory(String, mode: String, options: AVAudioSessionCategoryOptions)

广告,您可以通过以下方式使用它:

try? AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord, mode: AVAudioSessionModeDefault, options: AVAudioSessionPortBuiltInSpeaker)

暂无
暂无

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

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