Reputation: 101
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)?
Upvotes: 5
Views: 3351
Reputation: 61
Not sure if you've found a solution yet, but I was able to stream audio with AVAudioEngine. I didn't use AudioDecoder but instead streamed packets using URLSession to download packets. Then I streamed these downloaded packets into AudioFileStreamOpen where I formatted audio packets. Finally, I converted these packets into PCM buffers for the engine using AudioConverterFillComplexBuffer and scheduled these buffers into the engine for playback.
Here's my completed project: https://github.com/tanhakabir/SwiftAudioPlayer in which I set up a full engine that I used on my podcast player. And here's a short blog I wrote about the overall architecture: https://medium.com/chameleon-podcast/creating-an-advanced-streaming-audio-engine-for-ios-9fbc7aef4115
Here's the blog I based my library on: https://github.com/syedhali/AudioStreamer
Upvotes: 6
Reputation: 1150
try AVAudioConverter
let converter = AVAudioConverter(from: inputFormat, to: outputFormat)
Upvotes: 0