2
\$\begingroup\$

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)
\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

Seeing as your music is meant to loop indefinitely you could just assume the music is still playing after having started it once, something along the lines of:

need_music = true
def musica():
  if need_music:
    pygame.mixer.music.load( something )
    pygame.mixer.music.play(-1)
    need_music = false

From the look of your code your problem might be that the music isn't loading properly, by playing it only once you can tell if the problem is with the streaming, the music itself or your code and then figure out what is wrong from there.

\$\endgroup\$
0
\$\begingroup\$

The code looks mostly fine, although the more pythonic way to write it would be

def musica():
    if pygame.mixer.music,get_busy():
        print("Music is playing")
    else:
        music.play(loops=-1)

This could also eliminate any issues of it going into both if statements

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .