如何使用Node将PCM实时流式传输到网站?

时间:2018-11-19 11:56:00

标签: node.js socket.io discord discord.js

我将discord.js用于node.js。 Bot使用VoiceReceiver和createPCMStream()来获取pcm。我设法用这种方式制作了录音机:

const client = new Discord.Client({disableEveryone: false});
client.on("ready", function(){
    console.log(`${client.user.username} is online!`);
    client.user.setActivity("Ожидает!");
})

function generateOutputFile(channel, member) {
  // use IDs instead of username cause some people have stupid emojis in their name
  const fileName = `./recordings/${channel.id}-${member.id}-${Date.now()}.pcm`;
  return fs.createWriteStream(fileName);
}

client.on('message', message => {
  // Voice only works in guilds, if the message does not come from a guild,
  // we ignore it
  if (!message.guild) return;

  if (message.content === '/stream') {
    // Only try to join the sender's voice channel if they are in one themselves

    const voiceChannel = message.member.voiceChannel;

    if (message.member.voiceChannel) {
      message.member.voiceChannel.join()
        .then(connection => { // Connection is an instance of VoiceConnection
            const receiver = connection.createReceiver();

            connection.on('speaking', (user, speaking) => {
                  if (speaking) {
                    // this creates a 16-bit signed PCM, stereo 48KHz PCM stream.
                    const audioStream = receiver.createPCMStream(user);
                    // create an output stream so we can dump our data in a file
                    const outputStream = generateOutputFile(voiceChannel, user);
                    // pipe our audio data into the file stream
                    audioStream.pipe(outputStream);
                    outputStream.on("data", console.log);
                    // when the stream ends (the user stopped talking) tell the user
                    audioStream.on('end', () => {});
                  }
            })

            message.reply('Я тут, напишите /start, когда будете готовы начать!');
        })
        .catch(console.log);
    } else {
      message.reply('Сначала вы должны присоединиться к каналу!');
    }
  }
})

client.login(token);

但是我想通过此pcm的实时广播实时传送,或将其挂在端口上。请告诉我哪种方式。我看着扬声器,但没有弄清楚。

0 个答案:

没有答案