【问题标题】:Not able to set category for AVAudioSession无法为 AVAudioSession 设置类别
【发布时间】:2019-07-08 06:10:32
【问题描述】:

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

【问题讨论】:

  • 您使用的是哪个版本的Swift?最好将代码包含为 Text 而不是屏幕截图。
  • 我正在使用 swift 4

标签: ios swift avaudiosession avaudiorecorder


【解决方案1】:

如果你使用的是“Swift4”,那么设置类别的方法是不同的,即,

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-08
    • 2018-03-20
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-15
    相关资源
    最近更新 更多