我想使用 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
播放此集合。是否有更好的方式可以直接消耗音频数据(即时)?