使用AVAudioengine(iOS)播放流媒体音频

4

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


我的解码线性数据以Int16类型的数组缓存。因此,变量decodedData是[Int16]类型的,也许有可能直接使用这些数据?函数scheduleBuffer只允许AVAudioPCMBuffer作为输入。 - Tim
2个回答

5

不确定您是否已经找到了解决方案,但我能够使用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


0
尝试使用AVAudioConverter 让转换器 = AVAudioConverter(from: inputFormat, to: outputFormat)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接