From 7c543eeb2fc2fa18b0e1dce850aeaa6b65db2129 Mon Sep 17 00:00:00 2001 From: Abdo Date: Mon, 9 May 2022 04:08:34 +0300 Subject: [PATCH] Add the av_player_will_play_tags hook (#1842) A general version of the reviewer_will_play_question_sounds and reviewer_will_play_answer_sounds hooks --- qt/aqt/browser/previewer.py | 6 +++--- qt/aqt/clayout.py | 10 ++++++++-- qt/aqt/reviewer.py | 10 ++++------ qt/tools/genhooks_gui.py | 23 +++++++++++++++++++++++ 4 files changed, 38 insertions(+), 11 deletions(-) diff --git a/qt/aqt/browser/previewer.py b/qt/aqt/browser/previewer.py index c3eaf573b..ecd344cf1 100644 --- a/qt/aqt/browser/previewer.py +++ b/qt/aqt/browser/previewer.py @@ -216,11 +216,11 @@ class Previewer(QDialog): audio = c.question_av_tags() else: audio = c.answer_av_tags() - av_player.play_tags(audio) else: + audio = [] self._web.setPlaybackRequiresGesture(True) - av_player.clear_queue_and_maybe_interrupt() - + gui_hooks.av_player_will_play_tags(audio, self._state, self) + av_player.play_tags(audio) txt = self.mw.prepare_card_text_for_display(txt) txt = gui_hooks.card_will_show(txt, c, f"preview{self._state.capitalize()}") self._last_state = self._state_and_mod() diff --git a/qt/aqt/clayout.py b/qt/aqt/clayout.py index 57e154c5b..5d56c1646 100644 --- a/qt/aqt/clayout.py +++ b/qt/aqt/clayout.py @@ -548,10 +548,16 @@ class CardLayout(QDialog): audio = c.question_av_tags() else: audio = c.answer_av_tags() - av_player.play_tags(audio) else: + audio = [] self.preview_web.setPlaybackRequiresGesture(True) - av_player.clear_queue_and_maybe_interrupt() + side = "question" if self.pform.preview_front.isChecked() else "answer" + gui_hooks.av_player_will_play_tags( + audio, + side, + self, + ) + av_player.play_tags(audio) self.updateCardNames() diff --git a/qt/aqt/reviewer.py b/qt/aqt/reviewer.py index e200b41a3..b0e38d619 100644 --- a/qt/aqt/reviewer.py +++ b/qt/aqt/reviewer.py @@ -340,13 +340,12 @@ class Reviewer: self.web.setPlaybackRequiresGesture(False) sounds = c.question_av_tags() gui_hooks.reviewer_will_play_question_sounds(c, sounds) - av_player.play_tags(sounds) else: self.web.setPlaybackRequiresGesture(True) - av_player.clear_queue_and_maybe_interrupt() sounds = [] gui_hooks.reviewer_will_play_question_sounds(c, sounds) - av_player.play_tags(sounds) + gui_hooks.av_player_will_play_tags(sounds, self.state, self) + av_player.play_tags(sounds) # render & update bottom q = self._mungeQA(q) q = gui_hooks.card_will_show(q, c, "reviewQuestion") @@ -392,12 +391,11 @@ class Reviewer: if c.autoplay(): sounds = c.answer_av_tags() gui_hooks.reviewer_will_play_answer_sounds(c, sounds) - av_player.play_tags(sounds) else: - av_player.clear_queue_and_maybe_interrupt() sounds = [] gui_hooks.reviewer_will_play_answer_sounds(c, sounds) - av_player.play_tags(sounds) + gui_hooks.av_player_will_play_tags(sounds, self.state, self) + av_player.play_tags(sounds) a = self._mungeQA(a) a = gui_hooks.card_will_show(a, c, "reviewAnswer") # render and update bottom diff --git a/qt/tools/genhooks_gui.py b/qt/tools/genhooks_gui.py index 573c3305a..e39d3d906 100644 --- a/qt/tools/genhooks_gui.py +++ b/qt/tools/genhooks_gui.py @@ -925,6 +925,29 @@ gui_hooks.webview_did_inject_style_into_page.append(mytest) args=["player: aqt.sound.Player", "tag: anki.sound.AVTag"], ), Hook(name="av_player_did_end_playing", args=["player: aqt.sound.Player"]), + Hook( + name="av_player_will_play_tags", + args=[ + "tags: list[anki.sound.AVTag]", + "side: str", + "context: Any", + ], + doc="""Called before playing a card side's sounds. + + `tags` can be used to inspect and manipulate the sounds + that will be played (if any). + + `side` can either be "question" or "answer". + + `context` is the screen where the sounds will be played (e.g., Reviewer, Previewer, and CardLayout). + + This won't be called when the user manually plays sounds + using `Replay Audio`. + + Note that this hook is called even when the `Automatically play audio` + option is unchecked; This is so as to allow playing custom + sounds regardless of that option.""", + ), # Addon ################### Hook(