Audio playing problem

Thanks @dae

@congbosun I’m just an average mpv user for the most part who just uses mpv as a default video and audio player and happened to know a bit of coding. There were a number of changes to mpv in Anki 2.1.29 that I’m responsible for, but they were supposed to improve and add support for Windows and not break anything.

Once you start Anki, try to open the Debug Console (https://docs.ankiweb.net/#/misc?id=debug-console), copy-paste the following code and press Cmd+Return. Then close the Debug Console window and review your cards as usual. Once the audio can’t be played, please wait about 5 seconds and try to click the audio play button a few times. Then close Anki and upload somewhere mpv.log.txt from your Desktop, e.g. on https://gofile.io.

from aqt import mw
from aqt.sound import av_player, MpvManager, OnDoneCallback, SoundOrVideoPlayer
from anki.sound import AV_REF_RE, AVTag, SoundOrVideoTag
from aqt.mpv import MPV, MPVBase, MPVCommandError

# assert isinstance(av_player.players[0], MpvManager)

class MpvManager(MPV, SoundOrVideoPlayer):

    from anki.utils import isLin

    if not isLin:
        from aqt.mpv import MPVBase
        default_argv = MPVBase.default_argv + [
            "--input-media-keys=no",
        ]

    def __init__(self, base_path: str) -> None:
        from aqt.sound import _packagedCmd
        mpvPath, self.popenEnv = _packagedCmd(["mpv"])
        self.executable = mpvPath[0]
        self._on_done: Optional[OnDoneCallback] = None
        self.default_argv += ["--config-dir=" + base_path]
        super().__init__(window_id=None, debug=False)

    def play(self, tag: AVTag, on_done: OnDoneCallback) -> None:
        assert isinstance(tag, SoundOrVideoTag)
        self._on_done = on_done
        path = os.path.join(os.getcwd(), tag.filename)
        self.command("loadfile", path, "append-play")
        self.set_property("term-status-msg", "loadfile {}".format(path))
        gui_hooks.av_player_did_begin_playing(self, tag)

    def stop(self) -> None:
        self.set_property("term-status-msg", "stop {}".format(self._on_done))
        self.command("stop")

    def toggle_pause(self) -> None:
        self.set_property("term-status-msg", "set pause {}".format(not self.get_property("pause")))
        self.set_property("pause", not self.get_property("pause"))

    def seek_relative(self, secs: int) -> None:
        self.command("seek", secs, "relative")

    def on_end_file(self) -> None:
        self.set_property("term-status-msg", "on_end_file {}".format(self._on_done))
        if self._on_done:
            self._on_done()

    def shutdown(self) -> None:
        self.close()

    # Legacy, not used
    ##################################################

    togglePause = toggle_pause
    seekRelative = seek_relative


import aqt.sound

aqt.sound.mpvManager.shutdown()
aqt.sound.mpvManager = MpvManager(mw.pm.base)
path = os.path.expanduser("~/Desktop/mpv.log.txt")
aqt.sound.mpvManager.set_property("log-file", path)
av_player.players[0] = aqt.sound.mpvManager

print(path)
print('Done!')

The code contains a couple of additional log messages that maybe will help to narrow the issue down and eventually fix it.

2 Likes