我正在尝试在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}!`);
});发布于 2021-06-16 22:15:06
您的代码示例帮助我进行了不同的语法搜索。
在twilio博客示例应用程序中:https://www.twilio.com/blog/live-transcribing-phone-calls-using-twilio-media-streams-and-google-speech-text
该示例代码在web套接字中有用于“connection”、“start”和“stop”的case语句。也许这对你有帮助?
https://stackoverflow.com/questions/67417870
复制相似问题