【问题标题】:Can you stream Audio from a URL in a discord.js resource您可以从 discord.js 资源中的 URL 流式传输音频吗
【发布时间】: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,机器人显示一个绿色圆圈,但没有播放音频。 您对它的工作原理有什么想法吗?

【问题讨论】:

    标签: javascript discord.js


    【解决方案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)
    

    【讨论】:

      猜你喜欢
      • 2018-09-17
      • 2020-02-19
      • 1970-01-01
      • 1970-01-01
      • 2011-06-20
      • 1970-01-01
      • 2013-07-16
      • 2010-12-30
      • 1970-01-01
      相关资源
      最近更新 更多