From 8f2c49e2ae9f9ffe59393d8152462ef15f7d31a9 Mon Sep 17 00:00:00 2001 From: iamllama <100429699+iamllama@users.noreply.github.com> Date: Wed, 25 Dec 2024 00:00:33 +0800 Subject: [PATCH 1/5] stop audio playback on browser close --- qt/aqt/browser/browser.py | 1 + 1 file changed, 1 insertion(+) diff --git a/qt/aqt/browser/browser.py b/qt/aqt/browser/browser.py index 524887b08c1..7ccf592056f 100644 --- a/qt/aqt/browser/browser.py +++ b/qt/aqt/browser/browser.py @@ -401,6 +401,7 @@ def closeEvent(self, evt: QCloseEvent | None) -> None: def _closeWindow(self) -> None: assert self.editor is not None + av_player.stop_and_clear_queue() self._cleanup_preview() self._card_info.close() self.editor.cleanup() From 8acebd9f4e9c8e36e25022678ad026ba031c94f4 Mon Sep 17 00:00:00 2001 From: iamllama <100429699+iamllama@users.noreply.github.com> Date: Wed, 25 Dec 2024 01:41:07 +0800 Subject: [PATCH 2/5] revert fix --- qt/aqt/browser/browser.py | 1 - 1 file changed, 1 deletion(-) diff --git a/qt/aqt/browser/browser.py b/qt/aqt/browser/browser.py index 7ccf592056f..524887b08c1 100644 --- a/qt/aqt/browser/browser.py +++ b/qt/aqt/browser/browser.py @@ -401,7 +401,6 @@ def closeEvent(self, evt: QCloseEvent | None) -> None: def _closeWindow(self) -> None: assert self.editor is not None - av_player.stop_and_clear_queue() self._cleanup_preview() self._card_info.close() self.editor.cleanup() From 2d04b5767aa08f1115c931d9d081f1602a6254b2 Mon Sep 17 00:00:00 2001 From: iamllama <100429699+iamllama@users.noreply.github.com> Date: Wed, 25 Dec 2024 11:43:50 +0800 Subject: [PATCH 3/5] add caller-aware versions of play_file and stop_and_clear_queue --- qt/aqt/sound.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/qt/aqt/sound.py b/qt/aqt/sound.py index 36e3b2914cb..e377d001ee3 100644 --- a/qt/aqt/sound.py +++ b/qt/aqt/sound.py @@ -140,6 +140,8 @@ class AVPlayer: # when a new batch of audio is played, should the currently playing # audio be stopped? interrupt_current_audio = True + # caller key for the current playback (optional) + current_caller = None def __init__(self) -> None: self._enqueued: list[AVTag] = [] @@ -163,6 +165,10 @@ def stop_and_clear_queue(self) -> None: self._enqueued = [] self._stop_if_playing() + def stop_and_clear_queue_if_caller(self, caller) -> None: + if caller == self.current_caller: + self.stop_and_clear_queue() + def clear_queue_and_maybe_interrupt(self) -> None: self._enqueued = [] if self.interrupt_current_audio: @@ -171,6 +177,10 @@ def clear_queue_and_maybe_interrupt(self) -> None: def play_file(self, filename: str) -> None: self.play_tags([SoundOrVideoTag(filename=filename)]) + def play_file_with_caller(self, filename: str, caller) -> None: + self.current_caller = caller + self.play_file(filename) + def insert_file(self, filename: str) -> None: self._enqueued.insert(0, SoundOrVideoTag(filename=filename)) self._play_next_if_idle() @@ -198,6 +208,7 @@ def _pop_next(self) -> AVTag | None: return self._enqueued.pop(0) def _on_play_finished(self) -> None: + self.current_caller = None gui_hooks.av_player_did_end_playing(self.current_player) self.current_player = None self._play_next_if_idle() From db1adbd6308cc667053853f9692c1e2d6eb39906 Mon Sep 17 00:00:00 2001 From: iamllama <100429699+iamllama@users.noreply.github.com> Date: Wed, 25 Dec 2024 11:44:05 +0800 Subject: [PATCH 4/5] stop editor's audio autoplay on close --- qt/aqt/editor.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/qt/aqt/editor.py b/qt/aqt/editor.py index ff07de71160..e1b12036311 100644 --- a/qt/aqt/editor.py +++ b/qt/aqt/editor.py @@ -700,6 +700,7 @@ def fieldsAreBlank(self, previousNote: Note | None = None) -> bool: return True def cleanup(self) -> None: + av_player.stop_and_clear_queue_if_caller(self.editorMode) self.set_note(None) # prevent any remaining evalWithCallback() events from firing after C++ object deleted if self.web: @@ -850,7 +851,7 @@ def fnameToLink(self, fname: str) -> str: name = urllib.parse.quote(fname.encode("utf8")) return f'' else: - av_player.play_file(fname) + av_player.play_file_with_caller(fname, self.editorMode) return f"[sound:{html.escape(fname, quote=False)}]" def urlToFile(self, url: str) -> str | None: From d4322f96ed9a646b21fe2f146eb56f20a690d9e8 Mon Sep 17 00:00:00 2001 From: iamllama <100429699+iamllama@users.noreply.github.com> Date: Wed, 25 Dec 2024 13:37:25 +0800 Subject: [PATCH 5/5] remove superfluous stop_and_clear_queue from addcards --- qt/aqt/addcards.py | 1 - 1 file changed, 1 deletion(-) diff --git a/qt/aqt/addcards.py b/qt/aqt/addcards.py index a447b95c190..306ac910fdf 100644 --- a/qt/aqt/addcards.py +++ b/qt/aqt/addcards.py @@ -341,7 +341,6 @@ def closeEvent(self, evt: QCloseEvent) -> None: evt.ignore() def _close(self) -> None: - av_player.stop_and_clear_queue() self.editor.cleanup() self.notetype_chooser.cleanup() self.deck_chooser.cleanup()