ios - 无法为 AVAudioSession 设置类别

标签 ios swift avaudiosession avaudiorecorder

尝试使用 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!
    }
}

enter image description here

最佳答案

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

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

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

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

关于ios - 无法为 AVAudioSession 设置类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56929214/

相关文章:

iPhone 键盘没有显示

ios - 如何在 iOS Safari 上禁用前后缓存?

ios - AVFoundation AVAudioPlayer 停止暂停 - Objective-C

ios - 将泛型类作为参数传递给 swift 中的函数

ios - Swift 约定 - 警报或推送通知

ios - 如何解决 XCode 13.3 中的 SwiftPM.SPMRepositoryError 错误 3

iOS 13.0 UIMenu 和 UIContextMenuConfiguration 的 UIAction

avplayer - 错误域=NSOSStatusErrorDomain 代码=560030580 "The operation couldn’ 未完成。 (OSStatus 错误 560030580。)”

ios - 当我打开我的应用程序时 iTunes 音乐停止

iOS 在没有 Audio Session 的情况下播放视频