ios - 无法为 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)
推荐阅读
- java - 递归方法的理解(排列)
- python - 如何获得二值图像的高度、宽度、周长和面积?
- ios - 如何在音频线程之外使用通过使用 AVAudioEngine 获得的 PCM 缓冲区的数据?
- python-3.x - 使用 python 调度程序更改 RTSP 视频
- python - 对列表的熊猫列进行排序并调用其他列
- python-3.x - 使用输入输出文件在 Python 中进行并行处理
- vue.js - Vue.js 中的默认值选择类型
- excel - 使用excel VBA组合具有相同标题格式的多个文本文件?
- arrays - 从 csv 列表使用 Powershell 发送电子邮件
- wordpress - 在共享主机上获得专用 IP 是否明智?