你能从discord.js资源中的URL流式传输音频吗?

3

我该如何使用discord.js v13从URL播放音频。

我尝试了这段代码,但它没有起作用。

    const connection = joinVoiceChannel({
        channelId: channel_id.id,
        guildId: guild_id,
        adapterCreator: message.guild.voiceAdapterCreator,
    });

    const player = createAudioPlayer();
    const resource = createAudioResource('http://radioplayer.kissfmuk.com/live/')
    player.play(resource)

    connection.subscribe(player)

我已激活所有意图,机器人显示绿色圆圈,但没有声音播放。 你有什么想法吗?

2个回答

1
你试图将链接转换为资源,该链接必须返回一个音频文件。使用当前代码无法实现直播音频广播。如果url给出例如.mp3文件,则代码应该能够正常工作。
示例函数:
const Discord = require('discord.js');
const { Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_VOICE_STATES] });

const config =  require('./config/config.json');

const { joinVoiceChannel, createAudioPlayer, NoSubscriberBehavior, createAudioResource, AudioPlayerStatus, VoiceConnectionStatus, entersState } = require('@discordjs/voice');
const { join } = require('path');

client.once('ready', () => {
    console.log(config.onlineMessage);
    const channels = client.guilds.cache.find(f => f.name==="<Server Name>").channels;
    JoinChannel(channels.cache.find(r => r.name === "<Channel Name>"), './background.mp3', 0.025);  
});

client.login(config.token);

function JoinChannel(channel, track, volume) {
    const connection = joinVoiceChannel({
        channelId: channel.id,
        guildId: channel.guildId,
        adapterCreator: channel.guild.voiceAdapterCreator,
    });
    const player = createAudioPlayer();
    resource = createAudioResource(join(__dirname, track), { inlineVolume: true });
resource.volume.setVolume(volume);
    connection.subscribe(player); 
    connection.on(VoiceConnectionStatus.Ready, () => {console.log("ready"); player.play(resource);})
    connection.on(VoiceConnectionStatus.Disconnected, async (oldState, newState) => {
        try {
            console.log("Disconnected.")
            await Promise.race([
                entersState(connection, VoiceConnectionStatus.Signalling, 5_000),
                entersState(connection, VoiceConnectionStatus.Connecting, 5_000),
            ]);
        } catch (error) {
            connection.destroy();
        }
    });
    player.on('error', error => {
        console.error(`Error: ${error.message} with resource ${error.resource.metadata.title}`);
        player.play(getNextResource());
    });
    player.on(AudioPlayerStatus.Playing, () => {
        console.log('The audio player has started playing!');
    }); 
    player.on('idle', () => {
        connection.destroy();
    })
}

是否可以以 .mp3 结尾的链接? 比如:https://streams.ilovemusic.de/iloveradio2.mp3 - Jonathan Schuder
是的,这是可能的。 - Éndoxos
你能否更新你的回答,包括mp3信息?这将非常有帮助。 - WhooNo
@Éndoxos,你能给我一个代码示例吗?我无法工作。 - Jonathan Schuder

0
答案很简单,我只需要在使用资源之前订阅连接!
    const connection = joinVoiceChannel({
        channelId: channel_id.id,
        guildId: guild_id,
        adapterCreator: message.guild.voiceAdapterCreator,
    });


    const resource = 
    createAudioResource('https://streams.ilovemusic.de/iloveradio8.mp3', {
        inlineVolume: true
    })

    const player = createAudioPlayer();
    connection.subscribe(player)
    player.play(resource)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接