尝试使用 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)
关于ios - 无法为 AVAudioSession 设置类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56929214/