How to stream audio to the caller?

132 Views Asked by At

How can I send a stream of audio back to the caller?

I have a ReadableStream of bytes that correspond to audio data. I am retrieving this from my server on the fly.

I'd like to play this stream of audio back to the user from my Twilio voice function. Once it is done playing the audio, I'd like to continue with standard execution of my function.

const passThroughStream = new stream.PassThrough();
var readable = mp3.body; // mp3 is a stream of audio data I'm receiving on the fly
readable.pipe(passThroughStream);

As you can see, I have a readable stream. I don't want to have to wait for the whole stream to load, upload to an MP3 on the cloud, and then it. That is too much latency. I'd like to start playing the audio immediately as it streams in.

I also don't want to set up a bidrectional stream, I feel that would be overengineering. I just want to stream audio back to the user. I am just using standard to collect input from the user, which is more than enough.

Any help would be greatly appreciated.

1

There are 1 best solutions below

0
On

Twilio's Twiml Play verb requires a static file. For example, you can play a static audio file to Voicecall or to a Conference. I don't think there is any option to stream an audio file other setting up a Bi-Directional Media Stream (which you wrote you didn't want to do).