python - 检测歌曲是否正在播放
问题描述
如果当前正在播放一首歌曲,我试图将一首歌曲添加到列表中,并且在歌曲结束后,播放列表中的下一首歌曲,在那首歌曲结束后,播放下一首......
我找不到“歌曲播放”或“歌曲完成后”的语法或关键字
歌曲播放也很好,没有队列
我试过的
@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
解决方案
我没有看到一种方法来轮询当前正在播放的流。它们是独立的线程,完成后似乎不会进行任何报告。也许这会做,
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 中播放音轨队列
推荐阅读
- c# - 检测具有半径的 3D 点是否通过两个 3D 点的线?(C#)
- asp.net - ASP.NET 应用程序大约每 20 分钟回收一次
- laravel - 如何使用 maatwebsite excel 导出相关表中的数据?
- spring - 在 Spring Boot 运行时访问特定的属性文件
- git - 将 Git 存储库从 GCP 迁移到 AWS
- c# - 聚合来自两个不同数据库上下文的数据
- matlab - 如何通过插值从一对图像中制作中间图像
- database - Neo4j DBMS 能否将所有帐户和/或帐户类型的每个用户的并发会话数限制为组织定义的数量?
- java - Android Studio:应用程序在切换活动时崩溃
- wordpress - 在结帐时编辑 Woocommerce 订阅开始日期 + 频率