How to repeat sound?
I would like to play a sound continously (until a certain event).
I know to use sound.play_effect, but how can I issue another play_effect when the sound is over (basically, all I need to know is when the effect is over.
There appears to be no kind of function to test if a sound is playing, and it isn't possible to tell the
sound.play_effect()function to wait for the sound to stop. If you know the sound's length, you can use
time.sleep()as shown in the example in the
soundmodule docs. As long as you're not trying to execute code while the sound is playing it should work fine, because
time.sleep()will stop the entire script while the sound is played. Not sure if Python has any kind of threading support, as that would probably be necessary in that case.
There's a currently undocumented
Playerclass in the
soundmodule that allows (among other things) to loop sounds:
Here's a simple demo of how you can use it:
Note that the
Playerclass works with file paths instead of built-in sound effect names, so I'm using
os.path.expanduser()to get at the underlying .caf file of the built-in 'Beep' effect.
In case anybody else is dredging the forums trying to figure out how the
sound.play_effect()actually takes up to 5 parameters. The last one makes it repeat:
effect = sound.play_effect('my_sound.wav', volume, pitch, unknown, repeat)
If repeat is anything that evaluates to True, the sound seems to repeat until you call
I have no idea what the
See off-line doc, all parameters are explained (select play_effect and tap help)
As @dgelessus said in Speech/Sound Module Q's topic