2

我通过我的套接字连接以字节格式从服务器接收音频。

我正在尝试将它转换为 PCMFormat 来播放它。

func playAudio(data: NSData){

    let buffer = dataToPCMBuffer(format: format16KHzMono!, data: data)
    let player = AVAudioPlayerNode()
    self.audioEngine?.attach(audioPlayerNode)   
    let mixer = self.audioEngine?.mainMixerNode
    self.audioEngine?.connect(player, to: mixer!, format: format16KHzMono)

    self.playerQueue.async {
        self.audioPlayerNode.scheduleBuffer(buffer!) {
            print("stopping")
            if self.audioEngine!.isRunning  {
                self.audioPlayerNode.play()
            }else {
              try? self.audioEngine?.start()
        }
    }
    self.audioEngine?.prepare()
    try! self.audioEngine?.start()
   }

}

但我在下线面临崩溃。

self.audioEngine?.connect(player, to: mixer!, format: format16KHzMono)

希望,这是从字节流式传输音频的正确方法。

任何帮助将不胜感激。

4

0 回答 0