I want to stream audio on iOS using AVAudioEngine.
Currently I'm not sure how to do this.
I get RTP data from the network and want to playback this audio data with AVAudioEngine.
I am using the iOS Network.Framework to receive the network data.
I then decode the speech data and now want to play it back using AVAudioEngine.
Here is my receive code:
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)
}
}
How do I copy the decodedData to my AVAudioPCMBuffer?
Currently, my buffer var is created, but does not contain any data.
Background information:
My general approach would be to cache inside the PCMBuffer with a collection and playback this collection with the AVAudioEngine.
Is there a better way consume the audio data directly (instantly)?