我正在开发一个 iOS 应用程序。
我正在接收来自的原始数据流BLE
我在用着AVAudioEngine
带缓冲器
let format = AVAudioFormat(commonFormat: AVAudioCommonFormat.pcmFormatInt16, sampleRate: 8000.0, channels: 1, interleaved: false)!
我有采样率 8000 和线性 PCM 格式
使用以下代码初始化 AVM 音频引擎。
init() {
self.audioSession = AVCaptureSession()
self.engine = AVAudioEngine()
self.playerNode = AVAudioPlayerNode()
self.engine.attach(self.playerNode)
engine.connect(self.playerNode,
to: engine.outputNode,
format:AVAudioFormat.init(standardFormatWithSampleRate: 8000, channels: 1))
engine.prepare()
try! engine.start()
self.playerNode.play()
}
&使用缓冲区播放,使用下面的代码
func play(buffer: [Int16]) {
let interleavedChannelCount = 1
let frameLength = buffer.count / interleavedChannelCount
let audioBuffer = AVAudioPCMBuffer(pcmFormat: format, frameCapacity: AVAudioFrameCount(frameLength))!
// buffer contains 2 channel interleaved data
// audioBuffer contains 2 channel interleaved data
var buf = buffer
memcpy(audioBuffer.mutableAudioBufferList.pointee.mBuffers.mData, &buf, MemoryLayout<Int16>.stride * interleavedChannelCount * frameLength)
audioBuffer.frameLength = AVAudioFrameCount(frameLength)
self.playerNode.scheduleBuffer(audioBuffer) {
print("Played")
}
}
像这样填充缓冲区,
let i16array = decoded.withUnsafeBytes {
UnsafeBufferPointer<Int16>(start: $0, count: decoded.count).map(Int16.init(littleEndian:))
}
voicePlayer.play(buffer: i16array)
我收到噼啪声并且没有得到正确的输出。
我没有找到任何简单的解决方案。
请帮我纠正这个问题。
或者我使用下面的教程来播放原始音频,这种方法给了我声音,但它给了我噼啪的噪音。https://atastypixel.com/using-remoteio-audio-unit/ https://atastypixel.com/using-remoteio-audio-unit/
哪个是更好的方法。之后我可能还需要添加过滤器。请帮助任何建议/代码。