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.
Stop ALL effects
-
Is there any way to stop all playing sound effects without stopping the script?
-
@techteej There's only one undocumented sound feature as far as I know... see http://gist.github.com/omz/10023837
sound... [('Player', <type 'sound.Player'>), ('__doc__', None), ('__name__', 'sound'), ('__package__', None), ('load_effect', <built-in function load_effect>), ('play_effect', <built-in function play_effect>), ('set_volume', <built-in function set_volume>), ('stop_effect', <built-in function stop_effect>)] sound.Player... [('__class__', <type 'type'>), ('__cmp__', <slot wrapper '__cmp__' of 'sound.Player' objects>), ('__delattr__', <slot wrapper '__delattr__' of 'object' objects>), ('__doc__', 'Player objects'), ('__format__', <method '__format__' of 'object' objects>), ('__getattribute__', <slot wrapper '__getattribute__' of 'object' objects>), ('__hash__', <slot wrapper '__hash__' of 'object' objects>), ('__init__', <slot wrapper '__init__' of 'sound.Player' objects>), ('__new__', <built-in method __new__ of type object at 0xa313f4>), ('__reduce__', <method '__reduce__' of 'object' objects>), ('__reduce_ex__', <method '__reduce_ex__' of 'object' objects>), ('__repr__', <slot wrapper '__repr__' of 'object' objects>), ('__setattr__', <slot wrapper '__setattr__' of 'object' objects>), ('__sizeof__', <method '__sizeof__' of 'object' objects>), ('__str__', <slot wrapper '__str__' of 'object' objects>), ('__subclasshook__', <built-in method __subclasshook__ of type object at 0xa313f4>), ('current_time', <attribute 'current_time' of 'sound.Player' objects>), ('duration', <attribute 'duration' of 'sound.Player' objects>), ('number_of_loops', <attribute 'number_of_loops' of 'sound.Player' objects>), ('pause', <method 'pause' of 'sound.Player' objects>), ('play', <method 'play' of 'sound.Player' objects>), ('stop', <method 'stop' of 'sound.Player' objects>), ('volume', <attribute 'volume' of 'sound.Player' objects>)]
-
@techteej I've added the details above with all the available attributes.
-
@techteej It goes without saying I guess, that you could add the identifiers returned by 'play effect' to a list, and run through the list with 'stop effect' when you want to stop them all.
-
@tony Thanks for this.
-
@techteej Here's a wrapper class for sound that should add the function you need. I haven't tested it beyond the 4 lines at the end, but you should get the idea...
import sound as soundOrig class soundWrapper(object): def __init__(self, name = ''): self._l = list() soundOrig.__init__(name) def load_effect(self, name): soundOrig.load_effect(name) def play_effect(self, name, volume = 0.5, pitch = 1.0): iId = soundOrig.play_effect(name, volume, pitch) self._l.append(iId) return iId def stop_effect(self, effect_id): soundOrig.stop_effect(effect_id) self._l.remove(effect_id) def set_volume(self, vol): soundOrig.set_volume(vol) def stop_all_effects(self): for id in self._l: soundOrig.stop_effect(id) self._l = list() sound = soundWrapper() sound.play_effect('Beep') sound.play_effect('Beep') sound.stop_all_effects()
-
Good idea. I'd use a
dict
forsoundWrapper._l
(and a better name). -
@SpotlightKid Thanks! I'm only a month into python and using questions to push me through the learning curve... could you wise me up on the advantage of a dict here please? (The underscore on 'l' for list is because of the convention mentioned in the python tutorial (8.6) for attributes that are considered private and not part of the public interface of a class, that are subject to change without notice. I don't know if that's in common use but @ccc confirmed it made sense as best practice.)
-
I agree with the use of a leading underscore to mark an attribute as private. This is a useful convention for hiding implementation details from users of a class.
What I don't love is the use of single letter or two letter names for an attribute that will be used in multiple methods of a class. Instead of
self._l
, I would have gone forself._sounds
,self._current_sounds
, orself._sound_list
because these make the program easier to read, debug, and maintain. -
The trade off, of course, is line length... consider this line and what it would become...
self._pl = plistlib.readPlistFromString(self._strA[self._iS:self._iF]) self._ThisModuleSettingsPlist = plistlib.readPlistFromString(self._AllTextOfSourceFileString[self._StartOfTextFoundInteger:self._FinishOfTextFoundInteger])
Its a matter of personal preference (fortunately!), to my registration the first style makes the program more readable, but I've no argument with anyone whose preference is different.
-
@ccc Coding style aside... why would it be better to use a dict here?
-
The use of dict was @SpotlightKid 's suggestion and I must confess that I did not understand it as there is no lookup going on here.
-
@ccc Thanks
-
I just noticed that you used
list.remove()
to remove the sound's ID after it is stopped. That would be more efficient if_l
where a dict and you could just dodel self._l[effect_id]
. But since the list will probably be always very small, it doesn't really matter. -
@SpotlightKid Thanks