I have a continuous stream of raw PCM audio data from a TCP socket and I want to play them. I've done so many researches and saw many samples but no result. This gist was the most close solution but the problem is, it's streaming mp3 file. So I have a socket which receives linear PCM audio data and give them to the player like this:
func play(_ data: Data) {
// this function is called for every 320 bytes of linear PCM data.
// play the 320 bytes of PCM data here!
}
So is there any "Simple" way to play raw PCM audio data?
For iOS, you can use the RemoteIO Audio Unit or the AVAudioEngine with a circular buffer for real-time audio streaming.
You can't give network data directly to audio output, but instead should put it in a circular buffer from which an audio subsystem play callback can consume it at its fixed rate. You will need to pre-buffer some amount of audio samples to cover network jitter.
Simple "ways" of doing this might not handle network jitter gracefully.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With