您可以从 discord.js 资源中的 URL 流式传输音频吗

Posted

技术标签:

【中文标题】您可以从 discord.js 资源中的 URL 流式传输音频吗【英文标题】:Can you stream Audio from a URL in a discord.js resource 【发布时间】:2022-01-21 16:15:19 【问题描述】:

如何使用 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)

我已激活所有 Intent,机器人显示一个绿色圆圈,但没有播放音频。 您对它的工作原理有什么想法吗?

【问题讨论】:

【参考方案1】:

您尝试为其创建资源的链接必须返回一个音频文件。 实时音频广播不适用于您当前的代码。 如果网址给你例如.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 结尾?喜欢:streams.ilovemusic.de/iloveradio2.mp3 是的,这是可能的。 您会更新您的答案以包含 mp3 信息吗?很有帮助。 @Éndoxos 你能给我一个我不会工作的代码示例【参考方案2】:

答案很简单,我只需要在播放资源之前订阅连接!

    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)

【讨论】:

以上是关于您可以从 discord.js 资源中的 URL 流式传输音频吗的主要内容,如果未能解决你的问题,请参考以下文章

从带有 discord.js 的链接下载图像,然后删除该文件

如何在 discord.js 中发送超链接 [重复]

(Discord.js) Bot 删除所有发布的 URL

DISCORD.JS |我将如何执行升级和降级命令

您如何从单独的命令模块访问主要的 Discord“.js”

Discord.js 机器人在频道中的 10 条消息后发送消息