Strem Audio From Data Bytes

175 Views Asked by At

i am receiving audio from server in bytes format through my socket connection.

and i am trying it to convert to PCMFormat to play it.

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()
   }

}

but i am facing crash in below line.

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

hope, this is a right way to stream audio from bytes.

Any help will be appreciated.

0

There are 0 best solutions below