Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can one object instance stop sound that other instance is playing?
    primarykey
    data
    text
    <p>I am somewhat new to programming. I am building a simple app using Kivy framework. The app has 15 buttons. When you press the button, it starts playing a melody. If any other button is playing at that moment, it should stop, i.e. multiple sounds (from different instances) should not play at the same time.</p> <p>This is what I did — I created a class method that handles stopping and playing sounds. On press, the button instance passes sound object to this class method. However, on first press, since the sound object does not yet exist my app crashes. Hence the reason for <code>try … except AttributeError</code> part.</p> <pre><code>class Gumb(Button): soundfile = StringProperty(None) sound = ObjectProperty(None) now_playing_object = None def on_soundfile(self, instance, value): self.sound = SoundLoader.load(value) def on_press(self): if self.sound: self.__class__.play_sound(self.sound) @classmethod def play_sound(cls, new_sound_object): try: if cls.now_playing_object.state != 'stop': cls.now_playing_object.stop() except AttributeError: pass cls.now_playing_object = new_sound_object cls.now_playing_object.play() </code></pre> <p>This works, however I do not like it, especially the<code>try … except</code> part. There has to be a better way to do this. <em>Any ideas?</em></p> <p>Thanks!</p> <h2>Update 1:</h2> <p>@toto_tico provided me with a couple of solutions to circumvent the <code>try ... except</code> part.</p> <ol> <li><p>Use <code>if</code> instead of exception:</p> <pre><code>if cls.now_playing_object != None and cls.now_playing_object.state != 'stop': cls.now_playing_object.stop() </code></pre> <p>Although, after reading about efficiency of python's ifs vs exceptions, I am not sure that I should be trying to eliminate <code>try ... except</code> at all.</p></li> <li><p>Initialize the <code>now_playing_object</code> on the very beginning. To do this I used:</p> <pre><code>now_playing_object = SoundLoader.load(‘non_existant_file.wav’) </code></pre> <p>This works, but it feels a bit unreliable (it is strange that Kivy complains if the filename misses extension, but not if there is no file at all). <em>Nevertheless, I think I'll use this one.</em></p></li> </ol> <h2>Update 2:</h2> <p>It turns out that the solution was right in front of me. I just couldn't see it. There was really no need for <code>@classmethod</code>:</p> <pre><code>class Gumb(Button): ... def play_sound(self): if self.__class__.now_playing_object.state is not 'stop': self.__class__.now_playing_object.stop() self.__class__.now_playing_object = self.sound self.__class__.now_playing_object.play() </code></pre> <p>I've accepted the answer, but the real answer is in the comments. Thanks @toto_tico.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload