AVAudioEngine Realtime Audio Playing Issue

1k Views Asked by At

I am working on a push to talk functionality where sender can send an audio in form of bytes array to server and receiver can listen it at realtime through socket connection.

when i try to play video at receiver end using AVAudioEngine, it's not working.

    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: AVAudioFormat.init(commonFormat: AVAudioCommonFormat.pcmFormatInt16, sampleRate: 16000, channels: 1, interleaved: true) )

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

And, i am facing crash at below given line.

 self.audioEngine?.connect(player, to: mixer!, format: AVAudioFormat.init(commonFormat: AVAudioCommonFormat.pcmFormatInt16, sampleRate: 16000, channels: 1, interleaved: true) )

Any help will be appreciated.

1

There are 1 best solutions below

0
On

I think it’s the format in your connection. Try using nil instead. There are some magic numbers needed for sample rates, maybe 16000 is not one of them.