【问题标题】:Playing an audio file using discord.js and ytdl-core使用 discord.js 和 ytdl-core 播放音频文件
【发布时间】:2018-04-13 05:40:13
【问题描述】:

我正在尝试使用 ytdl 和 discord.js 下载并播放从 youtube 获取的音频文件:

        ytdl(url)
            .pipe(fs.createWriteStream('./music/downloads/music.mp3'));

        var voiceChannel = message.member.voiceChannel;
        voiceChannel.join().then(connection => {
            console.log("joined channel");
            const dispatcher = connection.playFile('./music/downloads/music.mp3');
            dispatcher.on("end", end => {
                console.log("left channel");
                voiceChannel.leave();
            });
        }).catch(err => console.log(err));
        isReady = true

我成功地在 ./music/downloads/ 中播放了没有 ytdl 部分的 mp3 文件 (ytdl(url).pipe(fs.createWriteStream('./music/downloads/music.mp3'));)。但是当那部分在代码中时,机器人就会加入和离开。

这是带有 ytdl 部分的输出:

Bot has started, with 107 users, in 43 channels of 3 guilds.
joined channel
left channel

这是没有 ytdl 部分的输出:

Bot has started, with 107 users, in 43 channels of 3 guilds.
joined channel
[plays mp3 file]
left channel

为什么会这样,我该如何解决?

【问题讨论】:

    标签: javascript node.js discord.js


    【解决方案1】:

    你这样做的效率很低。读写之间没有同步。
    等待文件写入文件系统,然后读取!

    直接串流

    YTDL的视频输出重定向到dispatcher,先转换成opus音频数据包,然后从你的电脑流式传输到Discord。

    message.member.voiceChannel.join()
    .then(connection => {
        console.log('joined channel');
    
        connection.playStream(ytdl(url))
        // When no packets left to send, leave the channel.
        .on('end', () => {
            console.log('left channel');
            connection.channel.leave();
        })
        // Handle error without crashing the app.
        .catch(console.error);
    })
    .catch(console.error);
    

    FWTR(先写后读)

    您使用的方法非常接近成功,但失败是您不同步读/写。

    var stream = ytdl(url);
    
    // Wait until writing is finished
    stream.pipe(fs.createWriteStream('tmp_buf_audio.mp3'))
    .on('end', () => {
        message.member.voiceChannel.join()
        .then(connection => {
            console.log('joined channel');
    
            connection.playStream(fs.createReadStream('tmp_buf_audio.mp3'))
            // When no packets left to send, leave the channel.
            .on('end', () => {
                console.log('left channel');
                connection.channel.leave();
            })
            // Handle error without crashing the app.
            .catch(console.error);
        })
        .catch(console.error);
    });
    

    【讨论】:

      【解决方案2】:

      当您需要播放音频流时,请使用 playStream 而不是 playFile。

      const streamOptions = { seek: 0, volume: 1 };
      var voiceChannel = message.member.voiceChannel;
              voiceChannel.join().then(connection => {
                  console.log("joined channel");
                  const stream = ytdl('https://www.youtube.com/watch?v=gOMhN-hfMtY', { filter : 'audioonly' });
                  const dispatcher = connection.playStream(stream, streamOptions);
                  dispatcher.on("end", end => {
                      console.log("left channel");
                      voiceChannel.leave();
                  });
              }).catch(err => console.log(err));
      

      【讨论】:

      • 有什么办法可以通过更改filter : 'audioonly'来观看视频吗?
      猜你喜欢
      • 1970-01-01
      • 2021-12-06
      • 2018-07-27
      • 1970-01-01
      • 2018-05-09
      • 2020-02-11
      • 2021-09-09
      • 2020-11-13
      • 1970-01-01
      相关资源
      最近更新 更多