Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop audio playback on editor close #3666

Merged
merged 6 commits into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion qt/aqt/addcards.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion qt/aqt/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -850,7 +851,7 @@ def fnameToLink(self, fname: str) -> str:
name = urllib.parse.quote(fname.encode("utf8"))
return f'<img src="{name}">'
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:
Expand Down
11 changes: 11 additions & 0 deletions qt/aqt/sound.py
Original file line number Diff line number Diff line change
Expand Up @@ -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] = []
Expand All @@ -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:
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand Down