-
Notifications
You must be signed in to change notification settings - Fork 10
/
cycleaudiodevice.lua
47 lines (43 loc) · 1.36 KB
/
cycleaudiodevice.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
--find devices with 'mpv --audio-device=help'
--set audio device on startup with --script-opts=audio=nicename
--or set on runtime with --script-message setaudiodevice nicename
mp.set_property("audio-fallback-to-null", "yes")
local index = 1
local devices = {
{ ['name'] = 'auto', ['nicename'] = 'default' },
{ ['name'] = 'openal/Built-in Audio Analog Stereo', ['nicename'] = 'headphones' },
{ ['name'] = 'pulse/alsa_output.pci-0000_06_00.0.analog-stereo', ['nicename'] = 'speaker' },
}
function setdevice()
mp.set_property("audio-device", devices[index]['name'])
mp.osd_message("Audio device set: "..devices[index]['nicename'])
end
function setdevicebymessage(message)
if message then
local match = false
for deviceindex in ipairs(devices) do
if devices[deviceindex]['nicename']:lower() == message:lower() then
index = deviceindex
match = true
break
end
end
if not match then index = tonumber(message) end
setdevice()
end
end
deviceonstart = mp.get_opt("audio")
if deviceonstart then
setdevicebymessage(deviceonstart)
end
mp.register_script_message("setaudiodevice", setdevicebymessage)
mp.add_key_binding("ctrl+A", "cycleaudiodevice", function()
if not #devices or not devices then return end
if #devices ~= index then
index = index + 1
setdevice()
else
index = 1
setdevice()
end
end)