Tony M
Tony M

Reputation: 15

pygame.mixer.Sound playing 2 times in a row

        if click[0] == 1:
            fabian_sound = pygame.mixer.Sound('my_sound.wav')
            pygame.mixer.Sound.stop
            pygame.mixer.Sound.play(my_sound, 1)
            pygame.mixer.Sound.set_volume(my_sound, 0.1)

Hi guys so I've made a simple code that if you click on image, my_sound.wav appears but theres one problem. Every time I click on image, my_sound.wav plays one time and after 2-3 sec again even if I dont click anything. Also it sounds like its slow down a bit can anyone help me with that? thanks!

Upvotes: 1

Views: 434

Answers (1)

sloth
sloth

Reputation: 101032

The sound is repeated 1 time because you set the loops argument to 1.

Look at the docs:

The loops argument controls how many times the sample will be repeated after being played the first time. A value of 5 means that the sound will be played once, then repeated five times, and so is played a total of six times. The default value (zero) means the Sound is not repeated, and so is only played once.

So if you set loops to 1, it will be repeated once.

Also, in the code you have posted:
- you load my_sound.wav, but never use it.
- The line pygame.mixer.Sound.stop also does nothing.
- Instead of pygame.mixer.Sound.play(my_sound, 1), you should call the method on the object itself, like my_sound.play() (note I also removed the loops argument). The same is true for the next line.

Upvotes: 1

Related Questions