【问题标题】:discord.py How do I play specific mp3s (or specific youtube videos) in discord voice channels?discord.py 如何在不和谐的语音频道中播放特定的 mp3(或特定的 youtube 视频)?
【发布时间】:2020-06-11 03:01:04
【问题描述】:

现在请记住,我是初学者,所以我不确定如何解决这个问题,而且许多教程都是预先重写的。非常感谢任何帮助!

【问题讨论】:

标签: discord discord.py


【解决方案1】:

确保为此安装 PyNaCl 并导入 naclos

mp3 示例:

@bot.command()
async def play(ctx):
    if ctx.author.voice.channel:
        if not ctx.guild.voice_client: # error would be thrown if bot already connected, this stops the error
            player = await ctx.author.voice.channel.connect()
        else:
            player = ctx.guild.voice_client
        player.play(discord.FFmpegPCMAudio("your.mp3")) # or "path/to/your.mp3"
    else:
        await ctx.send("Please connect to a voice channel.")

ytdl 示例:

@bot.command()
async def play(ctx, url=None):
    if ctx.author.voice.channel:
        if not ctx.guild.voice_client:
            player = await ctx.author.voice.channel.connect()
        else:
            player = ctx.guild.voice_client
        options = {
            "postprocessors":[{
                "key": "FFmpegExtractAudio", # download audio only
                "preferredcodec": "mp3", # other acceptable types "wav" etc.
                "preferredquality": "192" # 192kbps audio
            }],
            "format": "bestaudio/best",
            "outtmpl": "yt_song.mp3" # downloaded file name
        }
        with youtube_dl.YoutubeDL(options) as dl:
            dl.download([url])
        player.play(discord.FFmpegPCMAudio("yt_song.mp3"))
        playing = player.is_playing()
        while playing: # not compulsory
            await asyncio.sleep(1)
            playing = player.is_playing()
        os.remove("yt_song.mp3") # delete the file after use
    else:
        await ctx.send("Please connect to a voice channel.")

参考资料:

【讨论】:

    【解决方案2】:

    要播放 mp3 文件,您必须确保从链接 here 下载 FFMPEG。安装并解压缩后,将其添加到您的路径中

    添加到路径的说明 控制面板-->系统-->高级-->环境变量-->系统变量-->路径-->然后在刚才下载的文件中添加bin文件。

    安装后,您必须在代码中创建一个 VoiceClient 对象。有多种方法可以做到这一点。一个(我使用过的)是使用函数channel.connect() 加入频道并返回一个 VoiceClient 对象。一旦你有了一个连接客户端对象,你就可以使用函数VoiceClient.play()。参数(找到here)接受音频源。这就是 FFMPEG 的用武之地。在参数内部 --> VoiceClient.play(discord.FFmpegPCMAudio("myAudioFile.mp3"))

    如果此行无法找到 FFMPEG,您可以像这样插入可执行文件的路径 --> VoiceClient.play(discord.FFmpegPCMAudio( executable = "C:/ffmpeg/bin/ffmpeg.exe" , source = "myAudioFile.mp3")) 请注意,executable 字符串和 source 字符串会因您的 FFMPEG 位置以及您的 mp3 文件而异位置

    这就是示例代码在on_voice_state_update 函数中使用时的样子:

    @bot.event async def on_voice_state_update(member,before,after): VC = await after.channel.connect() VC.play(discord.FFmpegPCMAudio("MyAudioFile.mp3"))

    【讨论】:

      猜你喜欢
      • 2020-10-11
      • 2020-09-13
      • 2019-05-05
      • 2022-01-24
      • 2020-10-04
      • 2023-03-07
      • 2013-03-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多