我想使用AVAudioEngine在iOS上流式播放音频。目前我还不确定该怎么做。
我从网络上获取RTP数据,并想用AVAudioEngine播放这些音频数据。
我正在使用iOS Network.Framework接收网络数据。然后我对语音数据进行解码,现在希望使用AVAudioEngine回放它。
下面是我的接收代码:
connection.receiveMessage { (data, context, isComplete, error) in
if isComplete {
// decode the raw network data with Audio codec G711/G722
let decodedData = AudioDecoder.decode(enc: data, frames: 160)
// create PCMBuffer for audio data for playback
let format = AVAudioFormat(standardFormatWithSampleRate: 8000, channels: 1)
let buffer = AVAudioPCMBuffer(pcmFormat: format!, frameCapacity: 160)!
buffer.frameLength = buffer.frameCapacity
// TODO: now I have to copy the decodedData --> buffer (AVAudioPCMBuffer)
if error == nil {
// recall receive() for next message
self.receive(on: connection)
}
}如何将decodedData复制到我的AVAudioPCMBuffer?目前,我的buffer变量已创建,但不包含任何数据。
背景信息:
我的一般方法是使用集合在PCMBuffer中进行缓存,并使用AVAudioEngine回放该集合。
有没有更好的方式直接(即时)消费音频数据?
发布于 2019-11-27 05:51:39
不确定您是否找到了解决方案,但我可以使用AVAudioEngine流式传输音频。我没有使用AudioDecoder,而是使用URLSession来下载数据包。然后,我将这些下载的数据包流式传输到AudioFileStreamOpen中,在那里我对音频数据包进行格式化。最后,我使用AudioConverterFillComplexBuffer将这些数据包转换为用于引擎的PCM缓冲区,并将这些缓冲区调度到引擎中以供回放。
这是我完成的项目:https://github.com/tanhakabir/SwiftAudioPlayer,我在其中设置了一个完整的引擎,我在我的播客播放器上使用了它。下面是我写的一篇关于整体架构的简短博客:https://medium.com/chameleon-podcast/creating-an-advanced-streaming-audio-engine-for-ios-9fbc7aef4115
这是我的库所基于的博客:https://github.com/syedhali/AudioStreamer
发布于 2019-10-25 08:35:16
试试AVAudioConverter
让converter = AVAudioConverter(from: inputFormat,to: outputFormat)
https://stackoverflow.com/questions/58520714
复制相似问题