From 0ecc189a9bbac8c62f0b9a5145e52074d05e515f Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Sun, 15 Mar 2020 09:34:04 +1000 Subject: [PATCH] fix audio getting stuck (2/2) --- qt/aqt/sound.py | 38 ++++++++++++++++---------------------- qt/aqt/tts.py | 8 ++------ 2 files changed, 18 insertions(+), 28 deletions(-) diff --git a/qt/aqt/sound.py b/qt/aqt/sound.py index 2c783d72d5e..94b71032f18 100644 --- a/qt/aqt/sound.py +++ b/qt/aqt/sound.py @@ -217,10 +217,6 @@ def retryWait(proc: subprocess.Popen) -> int: ########################################################################## -class PlayerInterrupted(Exception): - pass - - class SimpleProcessPlayer(Player): # pylint: disable=abstract-method "A player that invokes a new process for each tag to play." @@ -231,7 +227,6 @@ def __init__(self, taskman: TaskManager) -> None: self._taskman = taskman self._terminate_flag = False self._process: Optional[subprocess.Popen] = None - self._lock = threading.Lock() def play(self, tag: AVTag, on_done: OnDoneCallback) -> None: self._terminate_flag = False @@ -260,23 +255,22 @@ def _wait_for_termination(self, tag: AVTag) -> None: ) while True: - with self._lock: - # should we abort playing? - if self._terminate_flag: - self._process.terminate() - self._process = None - return - - # wait for completion - try: - self._process.wait(0.1) - if self._process.returncode != 0: - print(f"player got return code: {self._process.returncode}") - self._process = None - return - except subprocess.TimeoutExpired: - # process still running, repeat loop - pass + # should we abort playing? + if self._terminate_flag: + self._process.terminate() + self._process = None + return + + # wait for completion + try: + self._process.wait(0.1) + if self._process.returncode != 0: + print(f"player got return code: {self._process.returncode}") + self._process = None + return + except subprocess.TimeoutExpired: + # process still running, repeat loop + pass def _on_done(self, ret: Future, cb: OnDoneCallback) -> None: try: diff --git a/qt/aqt/tts.py b/qt/aqt/tts.py index e3b91d89ad9..50c84761d2d 100644 --- a/qt/aqt/tts.py +++ b/qt/aqt/tts.py @@ -37,7 +37,7 @@ from anki.sound import AVTag, TTSTag from anki.utils import checksum, isWin, tmpdir from aqt import gui_hooks -from aqt.sound import OnDoneCallback, PlayerInterrupted, SimpleProcessPlayer +from aqt.sound import OnDoneCallback, SimpleProcessPlayer @dataclass @@ -234,11 +234,7 @@ def _play(self, tag: AVTag) -> None: self._wait_for_termination(tag) def _on_done(self, ret: Future, cb: OnDoneCallback) -> None: - try: - ret.result() - except PlayerInterrupted: - # don't fire done callback when interrupted - return + ret.result() # inject file into the top of the audio queue from aqt.sound import av_player