Audio playing problem

Hi, I’m using Anki 2.1.29 on macOS 10.14.6.
The audio can’t be played after several cards studied.
(Since I updated Anki)
I need to reopen Anki to recover it.
Thank you for developing Anki :slight_smile:

I presume you’re using the standard Anki version, not the one for older machines?

@kelciour is the resident mpv expert, and may have a suggestion

Yes, I downloaded it from top page this week, and I’m using 2017 macbook pro

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

Hi,
Here is the file.
Hope it helps:)
https://gofile.io/d/8iTajQ

2020年8月8日(土) 0:43 Nickolay Nonard via Anki Forums <anki2@discoursemail.com>:

1 Like

I’m not sure where’s the problem. At least, nothing seems to be wrong with Anki. At the same time, before Anki was closed, mpv seems to play 何月.mp3 several times just fine too.

At first, try to update mpv to the latest version.

  1. Close Anki.

  2. Download mpv-0.32.0.tar.gz from https://laboratory.stolendata.net/~djinn/mpv_osx/ and unpack it somewhere.

  3. Right-click on mpv.app and select “Show Package Contents”. Then go to mpv.app/Contents/MacOS.

    If “Show Package Contents” can’t be found, maybe it’s only available if app is installed, maybe try to rename mpv.app to mpv.zip or maybe try to access its content with “Go to location”, i.e. press Cmd+Shift+G and type the full path to the mpv.app or to the Contents subfolder.

    https://apple.stackexchange.com/a/310799

  4. Find Anki in the Applications folder.

  5. Right-click on Anki.app and select “Show Package Contents”. Then go to Anki.app/Contents/Resources/audio.

  6. Remove mpv and lib from the folder, i.e. from Anki.app/Contents/Resources/audio, and replace it with mpv and lib from mpv.app.

  7. Then restart Anki, open the Debug Console, copy-paste the following lines and press Command+Return. This is just to make sure that mpv version is 0.32.

    from aqt.sound import mpvManager
    print(mpvManager.get_property("mpv-version"))
1 Like

Hi, I don’t know if the solution resolves the issue or not,
but version 0.32 is VERY VERY convenient!!
Thank you

2020年8月13日(木) 1:01 Nickolay Nonard via Anki Forums <anki2@discoursemail.com>:

1 Like

Hi, the problem happened again
(With MPV 0.32)

2020年8月14日(金) 22:28 孫琮博 <congbosun@gmail.com>:

After I ran the code you told me when the problem happened, it got an error:


mpv timed out, restarting

Traceback (most recent call last):

File "aqt/mpv.py", line 350, in _get_response

File "queue.py", line 178, in get

_queue.Empty

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

File "aqt/mpv.py", line 377, in _send_request

File "aqt/mpv.py", line 354, in _get_response

aqt.mpv.MPVTimeoutError: unable to get response

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

File "aqt/mpv.py", line 350, in _get_response

File "queue.py", line 178, in get

_queue.Empty

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

File "aqt/main.py", line 1493, in onDebugRet

File "<string>", line 129, in <module>

File "aqt/mpv.py", line 616, in set_property

File "aqt/mpv.py", line 606, in command

File "aqt/mpv.py", line 384, in _send_request

File "aqt/mpv.py", line 377, in _send_request

File "aqt/mpv.py", line 354, in _get_response

aqt.mpv.MPVTimeoutError: unable to get response

2020年8月17日(月) 15:51 孫琮博 <congbosun@gmail.com>:

I’m not sure how it could have happened and how to reproduce it.

Since it seems to be some random crashes and can’t be narrowed down to the specific file, I’d suggest, to run this code in the Debug Console just one time to tell mpv to create its log file in ~/Library/Logs/mpv.anki.txt, and keep reviewing as usual.

import os

from aqt import mw
from aqt.sound import mpvManager

path = os.path.expanduser("~/Library/Logs/mpv.anki.txt")
with open(os.path.join(mw.pm.base, 'mpv.conf'), 'a') as f:
    f.write('log-file={}\n'.format(path))
mpvManager.shutdown()
print('log-file:', mpvManager.get_property('log-file'))

Or maybe this is not necessary and mpv already creates its log file in ~/Library/Logs/mpv.log.

A special case is the macOS bundle, it will create a log file at ~/Library/Logs/mpv.log by default.

When the audio stops working again, maybe in a day or a week, go to ~/Library/Logs and look at the last lines or upload ~/Library/Logs/mpv.anki.txt somewhere. Maybe it’ll contain some information what had happened. Maybe mpv just crashed while playing the audio file.

But do not close Anki. Instead, open the Debug Console, run this code and click the audio play button or review a few cards with audio. Maybe the audio will work again.

from aqt.sound import mpvManager
if mpvManager._on_done:
   mpvManager._on_done()

If it won’t work, try this code instead and click the audio play button.

from aqt.sound import av_player
av_player.current_player = None
1 Like

Thank you for the suggestion!
I’ll give it a try.

2020年8月18日(火) 5:16 Nickolay Nonard via Anki Forums <anki2@discoursemail.com>:

Hi,
The problem happened again, and I uploaded the log file:
https://gofile.io/d/MHBFuV

The _on_done() method works and the lines before I ran the method are:

[ 0.145][d][cplayer] Run command: stop, flags=64, args=[]
[ 0.146][v][osd/libass] Shaper: FriBidi 1.0.8 (SIMPLE) HarfBuzz-ng 2.6.4 (COMPLEX)
[ 0.146][v][osd/libass] Setting up fonts...
[ 0.147][v][osd/libass] Using font provider coretext
[ 0.147][v][osd/libass] Done.
[ 0.147][d][cplayer] Run command: enable-section, flags=64, args=["input_osc", "allow-hide-cursor+allow-vo-dragging"]
[ 0.147][d][cplayer] Run command: define-section, flags=64, args=["input_forced_osc", "", "force"]
[ 0.147][d][cplayer] Run command: enable-section, flags=64, args=["input_forced_osc", "allow-hide-cursor+allow-vo-dragging"]
[ 0.180][d][cplayer] Run command: disable-section, flags=64, args=["showhide"]
[ 0.180][d][cplayer] Run command: disable-section, flags=64, args=["showhide_wc"]
[ 0.808][d][cplayer] Run command: stop, flags=64, args=[]
[ 1.716][d][cplayer] Run command: stop, flags=64, args=[]
[ 2.185][d][cplayer] Run command: stop, flags=64, args=[]
[ 2.621][d][cplayer] Run command: stop, flags=64, args=[]
[ 2.897][d][cplayer] Run command: stop, flags=64, args=[]
[ 3.443][d][cplayer] Run command: stop, flags=64, args=[]
[ 4.169][d][cplayer] Run command: stop, flags=64, args=[]
[ 4.452][d][cplayer] Run command: stop, flags=64, args=[]
[ 4.697][d][cplayer] Run command: stop, flags=64, args=[]
[ 6.260][d][cplayer] Run command: stop, flags=64, args=[]
[ 8.722][d][cplayer] Run command: stop, flags=64, args=[]
[ 10.226][d][cplayer] Run command: stop, flags=64, args=[]
[ 11.081][d][cplayer] Run command: stop, flags=64, args=[]
[ 11.801][d][cplayer] Run command: stop, flags=64, args=[]
[ 12.242][d][cplayer] Run command: stop, flags=64, args=[]
[ 14.064][d][cplayer] Run command: stop, flags=64, args=[]
[ 76.151][d][cplayer] Run command: stop, flags=64, args=[]
[ 78.360][d][cplayer] Run command: stop, flags=64, args=[]
[ 90.203][d][cplayer] Run command: stop, flags=64, args=[]

2020年8月18日(火) 5:16 Nickolay Nonard via Anki Forums <anki2@discoursemail.com>:

Thank you. This is much better. For now, one last time, I guess, please install https://ankiweb.net/shared/info/881480056, ignore the description since you’ve already told mpv where to save the log file, restart Anki.and review cards as usual.

Once it happens again, upload the log file again. This time Anki won’t restart mpv and the log file won’t be overwritten. Maybe the last lines will contain some useful info or maybe not.

1 Like

Hi, I’m using Anki 2.1.33 and MPV 0.32
Now I can’t play audio even after restarting Anki.
The log is here (I accidentally overwrote the old one though)

https://gofile.io/d/CSRhYa

And maybe you can tell me how to debug.
I’m a software engineer and familiar with Python.

The log file looks good to me. According to it, just one file was played.

This is really strange. If it’s something related to Anki, restarting Anki should help all the time. Since there were no error messages after restarting Anki, it should mean that Anki was able to communicate with mpv just fine.

I’d try to,

  • Install Debug No Sound again if it was removed - https://ankiweb.net/shared/info/881480056 - to prevent Anki from restarting mpv and keep the log file as it is.

  • Once there’s no sound, click the audio replay button a few times, wait a few seconds seconds and look at the ‘mpv.debug.log.txt’ by opening it again in the text editor. The last loadfile command should contain the audio filename that was clicked.

If the log file had been changed on disk, Sublime Text will reload it automatically or ask to refresh the editor. It’s probably true for all popular text editors but I’m not sure and it might be safe to just open the log file again.

  • Then open the Debug Console in Anki, copy-paste this code and press Cmd+Return.
from aqt.sound import av_player, mpvManager
print("av_player.current_player:", av_player.current_player)
print("is_idle:", mpvManager.get_property("idle-active"))
av_player.current_player = None
print("av_player.current_player:", av_player.current_player)

Since no audio is playing, “current_player” should be None and “is_idle” should be True.

Maybe it’ll be different this time.

If current_player is not None and set to mpvManager, but is_idle is True, it means that mpv didn’t tell Anki that the current audio file is no longer being played, Anki never called _on_done callback to set current_player to None and Anki’s still thinking that mpv is busy playing the audio file. If this is the case, clicking the audio play button a few times should result in a number of “stop” commands in the log file.

If current_player is None and is_idle is True, but there’s no sound if you click the audio play button, I’d check the log file just to make sure that it’s being played by mpv.

If you know the audio filename in the collection.media folder, you could try to play it directly with mpv using the Debug Console in Anki.

from aqt.sound import mpvManager
filename = "youdao-49a9b3ef-7c0423c9-9fd3286e-3457bfca-1f91443c.mp3"
path = os.path.join(os.getcwd(), filename)
mpvManager.command("loadfile", path)

If there’s no sound, maybe locate mpv in Applications/Anki.app and try to play the audio file or maybe from the terminal app with /Applications/Anki.app/Contents/Resources/audio/mpv.

Maybe it’ll help to locate where is the problem.

tl;dr: if there’s no sound, click the audio play button a few times, run the code in the debug console to take a look at av_player.current_player and is_idle values and check the log file.

4 Likes