微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

直播音频会议 (Nodejs)

如何解决直播音频会议 (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-transcribing-phone-calls-using-twilio-media-streams-and-google-speech-text

该示例代码在 Web 套接字中包含用于“连接”、“开始”和“停止”的 case 语句。也许这有帮助?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。