Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
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 usetime.sleep()
as shown in the example in thesound
module docs. As long as you're not trying to execute code while the sound is playing it should work fine, becausetime.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
Player
class in thesound
module that allows (among other things) to loop sounds:Here's a simple demo of how you can use it:
https://gist.github.com/omz/10023837
Note that the
Player
class works with file paths instead of built-in sound effect names, so I'm usingos.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
module works,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
sound.stop_effect(effect)
.I have no idea what the
unknown
parameter does. -
See off-line doc, all parameters are explained (select play_effect and tap help)
As @dgelessus said in Speech/Sound Module Q's topic