How to Set a Fallback Audio Device in mpv?
This article explains how to configure the mpv media player to
automatically switch to a backup audio output if your primary device
becomes unavailable. While mpv does not feature a single “fallback”
command, you will learn how to achieve seamless audio switching using
the --audio-device prioritization syntax, custom profiles,
and specialized Lua scripts.
Understanding mpv’s Audio Device Handling
By default, mpv connects to your system’s default audio output. If that device is disconnected during playback, mpv’s behavior depends heavily on your operating system’s underlying audio subsystem (like PipeWire/PulseAudio on Linux, CoreAudio on macOS, or WASAPI on Windows). In many cases, the OS audio server will automatically route the stream to the next available default device. However, if you want to explicitly dictate which backup device mpv should choose, you need to configure it directly.
Method 1: The Multi-Device Option String
The most straightforward way to define a fallback is to pass a
comma-separated list of devices to the --audio-device
property. mpv will attempt to initialize the first device in the list;
if it fails or is disconnected, it moves to the next.
First, identify your exact device names by running the following command in your terminal or command prompt:
mpv --audio-device=helpOnce you have the exact device strings, open your
mpv.conf file and list them in order of preference:
# Specify primary device, followed by the fallback device
audio-device="wasapi/{primary-device-id},wasapi/{fallback-device-id}"Note: This method is highly effective for initial startup selection. However, if a device is unplugged during active playback, some audio backends may stall rather than dynamically falling back to the next item in the config line.
Method 2: Dynamic Switching via Lua Scripts
For true dynamic fallback—where mpv instantly detects a mid-playback disconnection and switches to a backup—a Lua script is the most robust solution. Because mpv exposes its properties via an API, a script can listen for errors or device changes.
You can create a simple script named audio-fallback.lua
and place it in your mpv scripts folder:
-- Automatically switch to default if the specified primary device fails
mpv.observe_property("audio-device", "string", function(name, val)
-- Monitor audio sync or playback errors here to trigger a switch
end)
mpv.register_event("playback-restart", function()
-- Logic to check if audio output is alive, otherwise fallback
if mpv.get_property("audio-device-list") == nil then
mpv.set_property("audio-device", "auto")
end
end)For a hands-off approach, setting your device to auto
(the default) and allowing a system-level utility (like
pavucontrol on Linux or the Windows Sound Control Panel) to
handle the fallback hierarchy usually provides the most seamless
mid-video transitions.