使用 AVAudioRecorder 录制 Opus iOS
Record Opus with AVAudioRecorder iOS
我正在尝试以 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 秒音频帖子的最佳方式是什么。
试试这个:
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:
方法中给你回调。希望对你有帮助。
以下用于重新编码 Opus 格式的配置有效。它不适用于 44100 Hz。仅适用于 16000 或 24000 Hz。
let recordSettings =
[AVNumberOfChannelsKey: 1,
AVFormatIDKey : kAudioFormatOpus,
AVSampleRateKey: 24000.0] as [String : Any]
没有其他变化,文件被正确保存,现在也调用了 didFinishRecoding。
我正在尝试以 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 秒音频帖子的最佳方式是什么。
试试这个:
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:
方法中给你回调。希望对你有帮助。
以下用于重新编码 Opus 格式的配置有效。它不适用于 44100 Hz。仅适用于 16000 或 24000 Hz。
let recordSettings =
[AVNumberOfChannelsKey: 1,
AVFormatIDKey : kAudioFormatOpus,
AVSampleRateKey: 24000.0] as [String : Any]
没有其他变化,文件被正确保存,现在也调用了 didFinishRecoding。