admin管理员组文章数量:1335662
I've integrated VAPI.AI to handle inbound and outbound calling, and everything is working fine. There’s an option to listen to any inbound or outbound call through a WebSocket URL, which provides a PCM stream. Here’s my current setup:
I’m receiving PCM audio data through a WebSocket and saving it to a file (audio.pcm) on the server. Afterward, I use FFmpeg to convert it to an MP3 file, which I can then play without any issues. Below is my current code for receiving and saving the audio data:
const WebSocket = require('ws');
const fs = require('fs');
let pcmBuffer = Buffer.alloc(0);
const ws = new WebSocket("wss://aws-us-west-2-production1-phone-call-websocket.vapi.ai/7420f27a-30fd-4f49-a995-5549ae7cc00d/transport");
ws.on('open', () => console.log('WebSocket connection established'));
ws.on('message', (data, isBinary) => {
if (isBinary) {
pcmBuffer = Buffer.concat([pcmBuffer, data]);
console.log(`Received PCM data, buffer size: ${pcmBuffer.length}`);
} else {
console.log('Received message:', JSON.parse(data.toString()));
}
});
ws.on('close', () => {
if (pcmBuffer.length > 0) {
fs.writeFileSync('audio.pcm', pcmBuffer);
console.log('Audio data saved to audio.pcm');
}
});
ws.on('error', (error) => console.error('WebSocket error:', error));
Now, my question is:
Is there a way to play the PCM audio directly in the Next.js frontend (client-side) by using a WebSocket listener, without saving it to a file first? I want to stream the audio directly to the client as it is received from the WebSocket.
I’ve tried different solutions, but they result in noisy audio that is not clear. The playback sounds like noise, not the intended voice.
I’ve considered converting PCM to MP3 on the server and sending it to the frontend, but I’d prefer a more direct approach that avoids saving and converting the file if possible.
Any suggestions or solutions would be greatly appreciated!
本文标签:
版权声明:本文标题:javascript - How to play PCM audio stream directly from WebSocket in Next.js frontend without saving to a file? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742379880a2463888.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论