javascript - 直播音频会议(Nodejs)
问题描述
我正在尝试将音频实时流式传输到 Twilio 会议,但不确定如何使其工作。
到目前为止,我已经创建了一个 Twilio 会议,当它开始时,它将开始流式传输并将数据发送到服务器以供 WebSocket 接收。现在我收到来自 Twilio 的以音频/x-mulaw 编码的媒体消息,但不清楚下一步我应该做什么。我看到了一个示例,它联系这些消息的有效负载并将它们转换为缓冲区,以重复接收到的音频。但我正在尝试实时流式传输这些数据,所以我不能真正做到这一点,因为我不知道音频什么时候会停止。那么有人可以向我解释一下我应该如何处理直播中的音频/x-mulaw 吗?非常感谢。:)
const express = require("express");
const app = express();
const http = require("http");
const WebSocket = require("ws");
const cors = require("cors");
const VoiceResponse = require("twilio").twiml.VoiceResponse;
const port = process.env.PORT || 8000;
const server = http.createServer(app);
const wss = new WebSocket.Server({ server });
app.use(express.urlencoded({ extended: true }));
app.use(cors());
wss.on("connection", function connection(ws) {
ws.on("message", (message) => {
message = JSON.parse(message);
switch (message.event) {
case "connected":
case "start":
// here comes the messages from all the conferences that are happening
// each message will have a callSid on start
// mediaFormat: { encoding: 'audio/x-mulaw', sampleRate: 8000, channels:1 }
console.log("callSid: ", message);
case "end":
break;
case "media":
// here messges will have the streamSid and a payload
console.log(message);
default:
break;
}
});
});
app.get(`/audio-room/conference`, (request, response) => {
const roomId = request.query["room_id"];
const voice = new VoiceResponse();
if (roomId) {
voice.start().stream({ url: "wss://4dbeaa4a0890.ngrok.io/" });
voice.dial().conference(
{
muted: true,
statusCallback: `https://4dbeaa4a0890.ngrok.io/audio-room/conference-callback?room-id=${roomId}`,
statusCallbackEvent: "start end join speaker mute",
statusCallbackMethod: "GET",
region: "us1",
waitUrl: "",
beep: false,
FriendlyName: roomId,
},
roomId.toString()
);
} else {
voice.say(
{
voice: "man",
language: "en-US",
},
`Please add room identity to your connection parameters`
);
}
response.send(voice.toString());
});
server.listen(port, function () {
console.log(`Server is listening on ${port}!`);
});
解决方案
您的代码示例帮助我进行了不同的语法搜索。
在 twilio 博客示例应用程序中:https ://www.twilio.com/blog/live-trancribing-phone-calls-using-twilio-media-streams-and-google-speech-text
该示例代码在 Web 套接字中包含用于“连接”、“开始”和“停止”的 case 语句。也许这有帮助?
推荐阅读
- socket.io - websocket传输的Socket.io问题
- javascript - 我的 FireBase 登录程序不适用于我的学校项目
- python - 在我的本地计算机上正确保存带有英文关键字的波斯文本
- javascript - 如何在逗号分隔的字符串中替换字符串计数逗号?
- boto3 - 如何从 SecretStringTemplate(旋转秘密 cloudformation 模板代码)中的文件递归地传递用户名作为参数?
- opengl-es-3.0 - OpenGL ES 3.2+ 是否有类似早期目标 alpha 测试的东西?
- angular - 当我单击角度按钮时如何实现基于推送的架构?
- c# - 无法获取 TCP 消息
- c++ - 找到连接两个移动对象的矩形的 4 个角
- javascript - 如何将 id 从删除按钮传递到弹出删除按钮