ios - 为什么 AVAudioRecorder 在使用 kAudioFormatMPEGLayer3 格式时会抛出错误?
问题描述
我正在尝试以 mp3 格式录制音频,但是当我使用时kAudioFormatMPEGLayer3
AVAudioRecorder
会引发错误。
代码是
获取文件路径
func getFileUrl() -> URL
{
let filename = "myRecording.mp3"
let filePath = getDocumentsDirectory().appendingPathComponent(filename)
return filePath
}
创造AVAudioRecorder
func setup_recorder()
{
if isAudioRecordingGranted
{
let session = AVAudioSession.sharedInstance()
do
{
try session.setCategory(AVAudioSessionCategoryPlayAndRecord, with: .defaultToSpeaker)
try session.setActive(true)
let settings = [
AVFormatIDKey: Int(kAudioFormatMPEGLayer3),
AVSampleRateKey: 44100,
AVNumberOfChannelsKey: 2,
AVEncoderAudioQualityKey:AVAudioQuality.high.rawValue
]
audioRecorder = try AVAudioRecorder(url: getFileUrl(), settings: settings)
audioRecorder.delegate = self
audioRecorder.isMeteringEnabled = true
audioRecorder.prepareToRecord()
}
catch let error {
display_alert(msg_title: "Error", msg_desc: error.localizedDescription, action_title: "OK")
}
}
else
{
display_alert(msg_title: "Error", msg_desc: "Don't have access to use your microphone.", action_title: "OK")
}
}
提前致谢。
解决方案
推荐阅读
- html - HTML - 禁用 Roboform 密码管理器
- java - java - 如何在java的测试类中调用创建的类?
- python - 在循环 Python 中添加变量以访问数据库
- reactjs - TypeError:渲染不是函数
- react-native - Flipper + React Native - 我可以调试 JavaScript 吗?
- angular - 表单组上的角度自定义交叉字段验证无法按预期的日期字段工作
- sql - 表的“三角关系”中的 SQL 查询
- java - 如何解决“找不到符号”的问题?
- c# - C# System.Text.Json JSON 值无法转换为类结构
- mysql - 我在 docker 上设置了 mysql 和 apache 超集并通过桥接网络连接,SQLAlchemy URI 是什么?