在pygame中检查歌曲是否播放完毕 [英] Check whether a song has finished playing in pygame

查看:267
本文介绍了在pygame中检查歌曲是否播放完毕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么办法可以判断一首歌在pygame中是否播放完毕?

Is there any way to tell whether a song has finished playing in 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()函数当我暂停歌曲时没有给我预期的输出.我想要的是在暂停歌曲时不打印 "歌曲完成".

Here, I used the pygame.mixer.music.get_busy() function to check whether the song has finished, but the problem is that the check_if_finished() function is not giving me the expected output when I pause the song. What I want is to not print "The song is finished" when I pause the song.

有没有办法在 pygame 中实现这一点?

Is there any way to achieve this in pygame?

如果有人能帮助我就好了.

It would be great if anyone could help me out.

推荐答案

我想要的是不打印歌曲完成了";当我暂停歌曲时.

What I want is to not print "The song is finished" when I pause the song.

你说得对.请参阅pygame.mixer.music.get_busy():

You are right. See pygame.mixer.music.get_busy():

当音乐流正在播放时返回 True.当音乐空闲时返回 False.在 pygame 2.0.1 及以上版本中,此函数在音乐暂停时返回 False.

Returns True when the music stream is actively playing. When the music is idle this returns False. In pygame 2.0.1 and above this function returns False when the music is paused.

您只需添加一个附加条件即可解决此问题:

You can deal with this by simply adding an additional condition:

def check_if_finished():

    if paused or pygame.mixer.music.get_busy():
        print("Song is not finished")
    else:
        print("Song is finshed")`

这篇关于在pygame中检查歌曲是否播放完毕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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