Switching from mpv to mplayer

Hello

I’m trying to set up Anki to use mplayer for audio playback instead of mpv without uninstalling mpv. I came across an instruction on GitHub that seemed promising (Replace mpv with mplayer · GitHub)

Here’s the code I used:

import aqt.sound

from aqt import mw, gui_hooks
from aqt.sound import SimpleMplayerSlaveModePlayer, av_player

def myfunc():
    av_player.players[0].shutdown()
    aqt.sound.mpvManager = None
    mplayer = SimpleMplayerSlaveModePlayer(mw.taskman, mw.col.media.dir())
    av_player.players[0] = mplayer

gui_hooks.profile_did_open.append(myfunc)

I created a folder named nompv in ~/.local/share/Anki2/addons21 and placed the code in a file named __init__.py within that folder. However, unfortunately, it didn’t work. :frowning:

I suspect the code might need to be updated to work with a more recent version of Anki, but I’m not familiar with Python and don’t know what should be changed.

If you could help me troubleshoot or update the code, I’d really appreciate it!

Thanks in advance!

1 Like

The code works for me.

If the add-on was added correctly, it should appear as enabled in Tools > Add-ons. In this case, it’ll be the name of the folder, i.e. nompv.

It can be also checked by running the following code in the debug console.

https://docs.ankiweb.net/misc.html#debug-console

from aqt.sound import av_player
print(av_player.players[0])

The output should be similar to this one.

<aqt.sound.SimpleMplayerSlaveModePlayer object at 0x0000024E40C98E20>
1 Like

Thank you for your response.
I believe I followed the instructions correctly, as nompv appeared among the other add-ons.
However, if I keep mpv, it won’t work, and I receive this error message:

Error
An error occurred. Please start Anki while holding down the shift key, which will temporarily disable the add-ons you have installed.
If the issue only occurs when add-ons are enabled, please use the Tools > Add-ons menu item to disable some add-ons and restart Anki, repeating until you discover the add-on that is causing the problem.
When you've discovered the add-on that is causing the problem, please report the issue to the add-on author.
Debug info:
Anki 2.1.66 (70506aeb) Python 3.13.1 Qt 6.8.1 PyQt 6.8.0
Platform: Linux-6.6.72-1-lts-x86_64-with-glibc2.40
Flags: frz=False ao=True sv=2
Add-ons, last update check: 2025-01-18 19:10:39

Caught exception:
Traceback (most recent call last):
  File "/usr/lib/python3.13/site-packages/aqt/mpv.py", line 373, in _send_request
    return self._get_response(timeout)
           ~~~~~~~~~~~~~~~~~~^^^^^^^^^
  File "/usr/lib/python3.13/site-packages/aqt/mpv.py", line 354, in _get_response
    raise MPVCommandError(message["error"])
aqt.mpv.MPVCommandError: invalid parameter

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.13/site-packages/aqt/webview.py", line 46, in cmd
    return json.dumps(self.onCmd(str))
                      ~~~~~~~~~~^^^^^
  File "/usr/lib/python3.13/site-packages/aqt/webview.py", line 153, in _onCmd
    return self._onBridgeCmd(str)
           ~~~~~~~~~~~~~~~~~^^^^^
  File "/usr/lib/python3.13/site-packages/aqt/webview.py", line 665, in _onBridgeCmd
    return self.onBridgeCmd(cmd)
           ~~~~~~~~~~~~~~~~^^^^^
  File "/usr/lib/python3.13/site-packages/aqt/overview.py", line 92, in _linkHandler
    self.mw.moveToState("review")
    ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^
  File "/usr/lib/python3.13/site-packages/aqt/main.py", line 713, in moveToState
    getattr(self, f"_{state}State", lambda *_: None)(oldState, *args)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.13/site-packages/aqt/main.py", line 734, in _reviewState
    self.reviewer.show()
    ~~~~~~~~~~~~~~~~~~^^
  File "/usr/lib/python3.13/site-packages/aqt/reviewer.py", line 166, in show
    self.refresh_if_needed()
    ~~~~~~~~~~~~~~~~~~~~~~^^
  File "/usr/lib/python3.13/site-packages/aqt/reviewer.py", line 186, in refresh_if_needed
    self.nextCard()
    ~~~~~~~~~~~~~^^
  File "/usr/lib/python3.13/site-packages/aqt/reviewer.py", line 248, in nextCard
    self._showQuestion()
    ~~~~~~~~~~~~~~~~~~^^
  File "/usr/lib/python3.13/site-packages/aqt/reviewer.py", line 377, in _showQuestion
    av_player.play_tags(sounds)
    ~~~~~~~~~~~~~~~~~~~^^^^^^^^
  File "/usr/lib/python3.13/site-packages/aqt/sound.py", line 150, in play_tags
    self._play_next_if_idle()
    ~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/usr/lib/python3.13/site-packages/aqt/sound.py", line 209, in _play_next_if_idle
    self._play(next)
    ~~~~~~~~~~^^^^^^
  File "/usr/lib/python3.13/site-packages/aqt/sound.py", line 216, in _play
    self.current_player.play(tag, self._on_play_finished)
    ~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.13/site-packages/aqt/sound.py", line 429, in play
    self.command("loadfile", path, "replace", "pause=no")
    ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.13/site-packages/aqt/mpv.py", line 595, in command
    return self._send_request({"command": list(args)}, timeout=timeout)
           ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.13/site-packages/aqt/mpv.py", line 375, in _send_request
    raise MPVCommandError(f"{message['command']!r}: {e}")
aqt.mpv.MPVCommandError: ['loadfile', '/home/user/.local/share/Anki2/User/collection.media/audio.ogg', 'replace', 'pause=no']: invalid parameter

I used to patch mpv.py and sound.py, but I am now using a binary version of Anki.

1 Like
from aqt.sound import av_player
print(av_player.players[0])

results in:

>>> from aqt.sound import av_player
... print(av_player.players[0])
<aqt.sound.SimpleMplayerSlaveModePlayer object at 0x792708451a90>

The output from the debug console looks good and mplayer should work, but my version of mplayer was compiled on 2019-08-28 and I can’t test the code with newer version.

If it doesn’t work, there’s no error message and no other add-on is interfering, maybe try replacing SimpleMplayerSlaveModePlayer with SimpleMplayerPlayer.


To fix mpv on Anki 2.1.66, the easiest option is to install the latest Anki version.

Alternatively, if you need Anki 2.1.66 for some reason, try the following __init__.py to make Anki work with recent mpv builds.

import aqt.mpv

def command(self, *args, timeout=1):
    args = list(args)
    if args[0] == "loadfile" and args[3] == "pause=no":
        args.insert(3, -1)
    return self._send_request({"command": args}, timeout=timeout)

aqt.mpv.MPV.command = command

1 Like

¡:scream:!
This is fantastic! It works perfectly!
I can’t thank you enough!

import aqt.mpv

def command(self, *args, timeout=1):
args = list(args)
if args[0] == “loadfile” and args[3] == “pause=no”:
args.insert(3, -1)
return self._send_request({“command”: args}, timeout=timeout)

aqt.mpv.MPV.command = command

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.