如何解决在pygame中检查歌曲是否播放完毕
有什么办法可以判断一首歌在pygame中是否播放完毕?
代码如下:
from tkinter import *
import pygame
root = Tk()
pygame.init()
def play():
pygame.mixer.music.load("test.ogg")
pygame.mixer.music.play(loops = 0)
def pause():
global paused
if paused == False:
pygame.mixer.music.pause()
paused = True
elif paused:
pygame.mixer.music.unpause()
paused = False
def check_if_finished():
if pygame.mixer.music.get_busy():
print("Song is not finished")
else:
print("Song is finshed")
paused = False
play_button = Button(root,text = "Play Song",command = play)
play_button.grid(row = 0,column = 0)
pause_button = Button(root,text = "Pause Song",command = pause)
pause_button.grid(row = 1,column = 0,pady = 15)
check_button = Button(root,text = "Check",command = check_if_finished)
check_button.grid(row = 2,column = 0)
mainloop()
在这里,我使用了 pygame.mixer.music.get_busy()
函数来检查歌曲是否已完成,但问题是当我暂停歌曲时,check_if_finished()
函数没有给我预期的输出。我想要的是在暂停歌曲时不打印 "The song is finished"
。
有没有办法在 pygame 中实现这一点?
如果有人能帮助我就好了。
解决方法
我想要的是在暂停歌曲时不打印“歌曲完成”。
你说得对。见pygame.mixer.music.get_busy()
:
当音乐流正在播放时返回 True。当音乐空闲时返回 False。在 pygame 2.0.1 及以上版本中,此函数在音乐暂停时返回 False。
您只需添加一个附加条件即可解决此问题:
def check_if_finished():
if paused or pygame.mixer.music.get_busy():
print("Song is not finished")
else:
print("Song is finshed")`
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。