Use mixer.music.get_busy()
to test test if any sound is being mixed.
from pygame import mixer
def mplayer(name):
''' for playing music '''
mixer.init()
mixer.music.load(name)
mixer.music.set_volume(0.7)
mixer.music.play()
mplayer('welcome.mp3')
while mixer.music.get_busy():
# you may have to handle the events here
# pygame.event.pump()
# [...]
pass
Note, you may need to handle the events in the loop that is waiting for the music to finish. See pygame.event.get()
respectively pygame.event.pump()
:
For each frame of your game, you will need to make some sort of call to the event queue. This ensures your program can internally interact with the rest of the operating system.
PyGame has 2 different modules for playing sound and music, the pygame.mixer module and the pygame.mixer.music module. This module contains classes for loading Sound objects and controlling playback. The difference is explained in the documentation:
The difference between the music playback and regular Sound playback is that the music is streamed, and never actually loaded all at once. The mixer system only supports a single music stream at once.
If pygame.mixer.music doesn't work for you try pygame.mixer.music:
from pygame import mixer
def mplayer(name):
''' for playing music '''
mixer.init()
my_sound = mixer.Sound(name)
my_sound.set_volume(0.7)
my_sound.play(0)
mplayer('welcome.mp3')
while mixer.get_busy():
# you may have to handle the events here
# pygame.event.pump()
# [...]
pass
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…