Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.5k views
in Technique[技术] by (71.8m points)

python - Check whether a song has finished playing in pygame

Is there any way to tell whether a song has finished playing in pygame?

Here's the code:

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()

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.

Is there any way to achieve this in pygame?

It would be great if anyone could help me out.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...