Reputation: 647
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.
Upvotes: 1
Views: 2620
Reputation: 211277
What I want is to not print "The song is finished" when I pause the song.
You are right. See pygame.mixer.music.get_busy()
:
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")`
Upvotes: 1