Live stream call audio in browser
@Vapi Support Bot @Vapi how to use call listenUrl in browser (react app) to listen to the live stream?
listenUrl, you can create a WebSocket connection to receive and play the audio data. Here's a basic outline of how you can achieve this:listenUrl const ws = new WebSocket(listenUrl);
ws.onopen = () => console.log("WebSocket connection established");
ws.onerror = (error) => console.error("WebSocket error:", error); const audioContext = new (window.AudioContext || window.webkitAudioContext)();
const audioQueue = [];
ws.onmessage = (event) => {
if (typeof event.data === 'object') {
event.data.arrayBuffer().then((buffer) => {
audioQueue.push(buffer);
});
}
}; const playAudio = () => {
if (audioQueue.length > 0) {
const buffer = audioQueue.shift();
audioContext.decodeAudioData(buffer, (decodedData) => {
const source = audioContext.createBufferSource();
source.buffer = decodedData;
source.connect(audioContext.destination);
source.start(0);
});
}
requestAnimationFrame(playAudio);
};
audioContext.onstatechange = () => {
if (audioContext.state === 'running') {
playAudio();
}
};