使用AVAudioengine(iOS)播放流式音频

问题描述 投票:1回答:1

我想使用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 var已创建,但不包含任何数据。

背景信息:

我一般的方法是将PCMBuffer缓存在一个集合中,并使用AVAudioEngine回放该集合。

是否有更好的方法直接(立即)使用音频数据?

ios audio streaming playback avaudioengine
1个回答
0
投票

尝试AVAudioConverter

让转换器= AVAudioConverter(从:inputFormat,到:outputFormat)

© www.soinside.com 2019 - 2024. All rights reserved.