我们正在开发一个可以记录和保存麦克风输入的应用程序。使用AVAudioRecorder不是一种选择,因为需要实时音频处理。
之所以使用AVAudioEngine,是因为它提供了对输入音频的低级访问。
let audioEngine = AVAudioEngine()
let inputNode = audioEngine.inputNode
let inputFormat = inputNode.inputFormat(forBus: 0)
inputNode.installTap(onBus: 0, bufferSize: AVAudioFrameCount(inputFormat.sampleRate * sampleInterval), format: inputFormat) { (buffer: AVAudioPCMBuffer, time: AVAudioTime) -> Void in
// sound preprocessing
// writing to audio file
audioFile.write(buffer.floatChannelData![0])
})我们的问题是录音太大了。对于5小时的录音,输出的音频文件为1.2 is的.caf格式。
let audioFile = AVAudioFile(forWriting: recordingPath, settings: [:], commonFormat: .pcmFormatFloat32, interleaved: isInterleaved)有没有一种很好的方法来压缩写入的音频文件?
默认采样频率为44100 is。我们将使用AVAudioMixerNode将输入下采样到20 the (在我们的例子中,较低的质量是可以接受的),但输出的大小在大小上是不可接受的。
录音包含大段的背景噪声。
有什么建议吗?
发布于 2018-09-01 05:17:25
.caf容器格式支持AAC压缩。通过将AVAudioFile设置字典设置为[AVFormatIDKey: kAudioFormatMPEG4AAC]来启用它
let audioFile = try! AVAudioFile(forWriting: recordingPath, settings: [AVFormatIDKey: kAudioFormatMPEG4AAC], commonFormat: .pcmFormatFloat32, interleaved: isInterleaved)还有其他影响文件大小和质量的设置键:AVSampleRateKey、AVEncoderBitRateKey和AVEncoderAudioQualityKey。
附注:您需要在使用完.caf文件后将其关闭。AVAudioFile没有显式的close()方法,因此您可以通过将对它的任何引用设为空来隐式关闭它。未压缩的.caf文件似乎可以在没有这个的情况下播放,但AAC文件却不能。
https://stackoverflow.com/questions/52111483
复制相似问题