首页 > 解决方案 > 检测歌曲是否正在播放

问题描述

如果当前正在播放一首歌曲,我试图将一首歌曲添加到列表中,并且在歌曲结束后,播放列表中的下一首歌曲,在那首歌曲结束后,播放下一首......

我找不到“歌曲播放”或“歌曲完成后”的语法或关键字

歌曲播放也很好,没有队列

我试过的

@commands.command()
  async def play(self, ctx, song):
    queue =[]
    await self.join(ctx)
    FFMPEG_OPTIONS = {'before_options':'-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5','options':'-vn'}
    YDL_OPTIONS = {'format':'bestaudio'}
    vc = ctx.voice_client
    if "https://" in song:
      songtype = "link"
    if songtype == "link":
      if queue == []:
        queue.append(song)
        playsong = queue.pop(0)
        with youtube_dl.YoutubeDL(YDL_OPTIONS) as ydl:
          info = ydl.extract_info(playsong, download=False)
          url2 = info['formats'][0]['url']
          source = await discord.FFmpegOpusAudio.from_probe(url2, **FFMPEG_OPTIONS)
          vc.play(source)
      else:
#Dont know where to go from here

标签: pythondiscord.pyyoutube-dl

解决方案


我没有看到一种方法来轮询当前正在播放的流。它们是独立的线程,完成后似乎不会进行任何报告。也许这会做,

await vc .play( source )
bot .commands .play( ctx, queue[0] ):

但如果不是,您必须从 ydl 解析该信息。在命令行上,选项是--get-duration.

根据从 Python 调用时的youtube-dl python 库文档 ,您将其通过 ydl_opts 作为'forceduration': opts .getduration,. '相信语法会是这样的:

ydl_opts = { 'forceduration': True }
duration = youtube_dl .YoutubeDL( ydl_opts )

您必须将其嵌套在您的另一个ydl呼叫下方,然后等待一个暂停该时间长度的计时器。可以仔细阅读 github 或 programcreek 上的不和谐机器人,看看他们是否以不同的方式进行操作。



编辑: 试试这个—— 在 discord.py 中播放音轨队列


推荐阅读