gpt4 book ai didi

javascript - 使用 discord.js 和 ytdl-core 播放音频文件

转载 作者:行者123 更新时间:2023-11-30 11:28:46 25 4
gpt4 key购买 nike

我正在尝试使用 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

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

最佳答案

使用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));

关于javascript - 使用 discord.js 和 ytdl-core 播放音频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47045805/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com