【问题标题】:Playback streaming audio with AVAudioengine (iOS)使用 AVAudioengine (iOS) 播放流式音频
【发布时间】:2019-10-23 10:23:53
【问题描述】:

我想在 iOS 上使用 AVAudioEngine 流式传输音频。 目前我不确定如何执行此操作。

我从网络获取 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 播放这个集合。

有没有更好的方法直接(即时)消费音频数据?

【问题讨论】:

  • 我解码的线性数据从 Int16 类型的数组中兑现。所以 var decodedData 来自 [Int16] 类型,也许有可能直接使用这些数据? scheduleBuffer 函数只允许 AVAudioPCMBuffer 作为输入。

标签: ios audio streaming playback avaudioengine


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    试试 AVAudioConverter

    让转换器 = AVAudioConverter(from: inputFormat, to: outputFormat)

    【讨论】:

      猜你喜欢
      • 2016-02-27
      • 1970-01-01
      • 2021-09-14
      • 2015-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-13
      • 2023-03-23
      相关资源
      最近更新 更多