import pygame
pygame.mixer.init()
pygame.mixer.music.load(file)#any mp3 file
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
pygame.time.Clock().tick()
I know this is the code to play the music in python with pygame Can you explain why we use
while pygame.mixer.music.get_busy():
pygame.time.Clock().tick()
Basically, the play()
function is asynchronous .It means it won't wait for a return message to continue. And when the program execution reaches the end, mixer object will be destroyed and the music will stop. To avoid that we use
while pygame.mixer.music.get_busy():
pygame.time.Clock().tick()
to run the while loop until the music finishes. Then the mixer object will not be destroyed until the music playing finishes. get_busy()
method is used to check if the music stream is playing.
from pygame import*
mixer.init()
mixer.music.load('music filename or path with extension .mp3')
mixer.music.play()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.