播放音频时,最后一部分被切断.如何解决?(discord.py) [英] When playing audio, the last part is cut off. How can this be fixed? (discord.py)

查看:83
本文介绍了播放音频时,最后一部分被切断.如何解决?(discord.py)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在生产的漫游器,并且已经弄清楚了如何使其从youtube播放音频.音频流,因此文件不下载到我的PC.这是我的代码:

I have a bot I am producing and I have figured out how to make it play audio from youtube. The audio is streamed so the files are not downloaded to my PC. Here is my code:

@bot.command(name='play', aliases=['p'], help='Plays a song.')
async def play(ctx, url):
    channel = ctx.message.author.voice.channel
    if ctx.guild.voice_client is None:
        await channel.connect()
    client = ctx.guild.voice_client
    player = await YTDLSource.from_url(url, stream = True)
    ctx.voice_client.play(player)
    await ctx.send('Now Playing: {}'.format(player.title))

我正在使用一些未在此块中显示的代码,因为它是basic_voice.py包的一部分(在此处找到:

I am using some code that is not shown in this block because it is part of the basic_voice.py package (found here: https://github.com/Rapptz/discord.py/blob/master/examples/basic_voice.py, I am using lines 12-52). My issue is that the audio is cut off at the end, with the FFMPEG window closing on my PC. This happened when I tested local files on my PC as well. I am not sure why FFMPEG just closes early, but I'd like a fix to it if possible. Also, if it's important, the amount cut off at the end is dependant on the length of the audio being played. The player works with no lag, it just mysteriously stops.

推荐答案

当您尝试制作一个不下载正在播放的歌曲的机器人时,这是一个已知问题.此处说明:

This is a known issue when you try to make a bot which doesn't download the song it's playing. It is explained here : https://support.discord.com/hc/en-us/articles/360035010351--Known-Issue-Music-Bots-Not-Playing-Music-From-Certain-Sources

要解决此问题,您可以使用discord.py中的 FFmpegPCMAudio 方法并提供特定的选项,以便机器人可以重新连接并继续播放视频:

To solve the problem, you can use the FFmpegPCMAudio method from discord.py and give specific options so the bot will be able to reconnect and continue to play the video :

ydl_opts = {'format': 'bestaudio'}
FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}

@bot.command(name='play', aliases=['p'], help='Plays a song.')
async def play(ctx, url):
    channel = ctx.message.author.voice.channel
    voice = get(self.bot.voice_clients, guild=ctx.guild)

    if voice and voice.is_connected():
        await voice.move_to(channel)
    else:
        voice = await channel.connect()
    
    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
            source = ydl.extract_info(url, download=False)['formats'][0]['url']

    voice.play(discord.FFmpegPCMAudio(song['source'], **FFMPEG_OPTIONS))
    voice.is_playing

这篇关于播放音频时,最后一部分被切断.如何解决?(discord.py)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆