Philipp Hörist pushed to branch master at gajim / gajim


Commits:
84474aae by lovetox at 2021-12-21T10:38:51+01:00
Sound: Don’t play sound when loop is in progress

- - - - -


1 changed file:

- gajim/common/sound.py


Changes:

=====================================
gajim/common/sound.py
=====================================
@@ -44,14 +44,24 @@ def play(self, path: Path, loop: bool = False) -> None:
     def stop(self) -> None:
         raise NotImplementedError
 
+    def loop_in_progress(self) -> bool:
+        raise NotImplementedError
+
 
 class PlatformWindows(PlaySound):
 
+    def __init__(self) -> None:
+        self._loop_in_progress = False
+
     def play(self, path: Path, loop: bool = False) -> None:
+        if self._loop_in_progress:
+            return
+
         flags = (winsound.SND_FILENAME |
                  winsound.SND_ASYNC |
                  winsound.SND_NODEFAULT)
         if loop:
+            self._loop_in_progress = True
             flags = flags | winsound.SND_LOOP
 
         try:
@@ -64,6 +74,10 @@ def stop(self) -> None:
             winsound.PlaySound(None, 0)
         except Exception:
             log.exception('Sound Playback Error')
+        self._loop_in_progress = False
+
+    def loop_in_progress(self) -> bool:
+        return self._loop_in_progress
 
 
 class PlatformMacOS(PlaySound):
@@ -76,6 +90,9 @@ def play(self, path: Path, loop: bool = False) -> None:
     def stop(self) -> None:
         pass
 
+    def loop_in_progress(self) -> bool:
+        return False
+
 
 class PlatformUnix(PlaySound):
 
@@ -86,8 +103,10 @@ def play(self, path: Path, loop: bool = False) -> None:
         if not app.is_installed('GSOUND'):
             return
 
+        if self.loop_in_progress():
+            return
+
         attrs = {'media.filename': str(path)}
-        self._cancellable = Gio.Cancellable()
 
         if loop:
             self._play_loop(attrs)
@@ -99,6 +118,7 @@ def play(self, path: Path, loop: bool = False) -> None:
                 self._cancellable = None
 
     def _play_loop(self, attrs: dict[str, str]) -> None:
+        self._cancellable = Gio.Cancellable()
         try:
             app.gsound_ctx.play_full(attrs,
                                      self._cancellable,
@@ -117,6 +137,7 @@ def _on_play_finished(self,
         except GLib.Error as error:
             quark = GLib.quark_try_string('g-io-error-quark')
             if error.matches(quark, Gio.IOErrorEnum.CANCELLED):
+                self._cancellable = None
                 return
             log.error('Could not play sound: %s', error.message)
 
@@ -128,6 +149,9 @@ def stop(self) -> None:
         self._cancellable.cancel()
         self._cancellable = None
 
+    def loop_in_progress(self) -> bool:
+        return self._cancellable is not None
+
 
 def _init_platform() -> PlaySound:
     if sys.platform == 'win32':



View it on GitLab: 
https://dev.gajim.org/gajim/gajim/-/commit/84474aae9c1320d0880396261bbb79b8272c8687

-- 
View it on GitLab: 
https://dev.gajim.org/gajim/gajim/-/commit/84474aae9c1320d0880396261bbb79b8272c8687
You're receiving this email because of your account on dev.gajim.org.


_______________________________________________
Commits mailing list
[email protected]
https://lists.gajim.org/cgi-bin/listinfo/commits

Reply via email to