Reputation: 829
I'm trying to play background music and the music keeps on starting. I tried to use music.busy
, but I can't figure it out. I want it to check if sound/music is playing, then I want it to print "music is playing". If a sound/music isn't playing I want it to start up a new song and loop it.
def musica():
if pygame.mixer.music.get_busy() == True:
print("music is playing")
if pygame.mixer.music.get_busy() == False:
music.play(loops=-1)
Upvotes: 0
Views: 1154
Reputation: 1
if you are looking to play a sound during a specific event in a loop (e.g. opening a menu), it is possible to play sound only once by using timers.
example:
sound = pygame.mixer.Sound('sound.wav')
playSound = True
playSound_counter = 0
while:
playSound_counter += 1
if playSound_counter >= 11:
playSound = True
playSound_counter = 0
if i.rect.collidepoint(mouse_position):
if playSound == True :
sound.play()
playSound = False
elif playSound_counter == 10:
playSound_counter = 0
This is essentially two timers - one sets sound back to True every 11 secs, another negates it while mouse is over a rect.
by "playing once" i meant to be able to play it again in a controlled way when a certain event is triggered
Upvotes: 0
Reputation: 829
I ended up doing this it is more of a workaround but it works:
m = 0
def musica():
global m
if m == 0:
music.play(loops=-1)
m = 1
Upvotes: 0
Reputation: 6966
An easy way to play looped sounds is winsound. See the python documentation (https://docs.python.org/3.4/library/winsound.html) and set the parameter 'flags' in PlaySound to winsound.SND_LOOP.
Upvotes: 2