我试图在iOS中录制Opus格式。当我使用LinearPCM格式化测试记录器,即wav
时,正确调用AVAudioRecorderDelegate
函数和updateMeters,并且记录器URL在给定路径上保存有效文件。这是我用于重新编码的配置。
let fileMgr = FileManager.default
let dirPaths = fileMgr.urls(for: .documentDirectory,
in: .userDomainMask)
let soundFileURL = dirPaths[0].appendingPathComponent("audio.wav")
let recordSettings =
[AVEncoderAudioQualityKey: AVAudioQuality.low.rawValue,
AVEncoderBitRateKey: 16,
AVNumberOfChannelsKey: 1,
AVFormatIDKey : kAudioFormatLinearPCM,
AVSampleRateKey: 44100.0] as [String : Any]
然后我尝试使用以下配置测试Opus,但是没有调用委托函数,米值是常数-160db(沉默)而audioRecorder
url是nil
因为我可以'在委托函数中得到它:
func audioRecorderDidFinishRecording(_ recorder: AVAudioRecorder, successfully flag: Bool) {
print(audioRecorder!.currentTime)
self.audioFileUrl = audioRecorder!.url
}
我使用的Opus配置:
let soundFileURL = dirPaths[0].appendingPathComponent("audio.opus")
let recordSettings =
[AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue,
AVEncoderBitRateKey: 16,
AVNumberOfChannelsKey: 1,
AVFormatIDKey : kAudioFormatOpus,
AVSampleRateKey: 44100.0] as [String : Any]
如何正确设置配置以录制我的音频社交网络应用的Opus格式。什么是最好的30秒音频帖子上传和听取其他用户的一面。
答案 0 :(得分:1)
以下用于重新编码Opus格式的配置有效。它不能在44100赫兹上工作。仅适用于16000或24000 Hz。
let recordSettings =
[AVNumberOfChannelsKey: 1,
AVFormatIDKey : kAudioFormatOpus,
AVSampleRateKey: 24000.0] as [String : Any]
其他任何内容都没有更改,文件正确保存,现在也调用了didFinishRecoding。
答案 1 :(得分:0)
试试这个:
var recordSettings = Dictionary() as [String: Any]
var audioRecorder: AVAudioRecorder?
func recordAudio() {
let fileMgr = FileManager.default
let dirPaths = fileMgr.urls(for: .documentDirectory,
in: .userDomainMask)
let soundFileURL = dirPaths[0].appendingPathComponent("audio.wav")
recordSettings =
[AVEncoderAudioQualityKey: AVAudioQuality.low.rawValue,
AVEncoderBitRateKey: 16,
AVNumberOfChannelsKey: 1,
AVFormatIDKey : kAudioFormatLinearPCM,
AVSampleRateKey: 44100.0] as [String : Any]
do {
audioRecorder = try AVAudioRecorder(url: soundFileURL, settings: recordSettings)
} catch {
print(error.localizedDescription)
}
audioRecorder?.delegate = self
audioRecorder?.prepareToRecord()
// call record method when you want to start recording
audioRecorder?.record()
}
func audioRecorderDidFinishRecording(_ recorder: AVAudioRecorder, successfully flag: Bool) {
print(recorder.currentTime)
do {
audioPlayer = try AVAudioPlayer(contentsOf: recorder.url)
} catch {
print(error)
}
audioPlayer?.play()
}
当您想要停止录制时,需要致电audioRecorder?.stop()
。它将以audioRecorderDidFinishRecording(_ recorder:
方法为您提供回调。希望它有所帮助。