First I managed to read the system volume with this edit to /usr/share/vlc/lua/http/requests/status.xml
Code: Select all
> -- Fetch current system volume
> local function get_system_volume()
> local handle = io.popen("amixer get Master | grep -o '[0-9]*%' | head -1 | tr -d '%'")
> if handle then
> local sys_volume = handle:read("*a")
> handle:close()
> return sys_volume:gsub("\n", "") or "50" -- Remove newlines, default to 50
> else
> return "50"
> end
> end
>
37a50,51
>
>
38a53,54
>
> print("<systemvolume>" .. get_system_volume() .. "</systemvolume>\n")Then I moved to writing a lua extension, with the hope to send a command with something like http://pi.local:2222/set_volume?volume=15 but so far I only got 404.
Below is my current draft of /usr/share/vlc/lua/extensions/system_volume.lua - but the logs say
Code: Select all
Trying Lua playlist script /usr/share/vlc/lua/extensions/system_volume.luaAny tip or working sample I could use to better understand?
EDIT: I just noticed I did not check "Lua interpreter" under Extra interface modules, but it didn't make any difference.
Also, I'm not sure about the dropdown "Interface module". Should I pick Lua interpreter there?
Code: Select all
# cat system_volume.lua
-- Define descriptor() FIRST, at the top level:
function descriptor()
return {
title = "System Volume Control",
version = "1.0",
author = "Your Name",
description = "Control system volume using amixer",
capabilities = {} -- Return an EMPTY table for capabilities
}
end
-- Then define the rest of your functions:
local volume_extension = {
set_system_volume = function(volume)
local handle = io.popen("amixer set Master " .. volume .. "%")
local result = handle:read("*a")
handle:close()
if result then
vlc.msg.info("[Volume Extension] System volume set to " .. volume .. "%")
return true
else
vlc.msg.err("[Volume Extension] Error setting system volume: " .. (result or "Unknown error"))
return false
end
end,
get_system_volume = function()
local handle = io.popen("amixer get Master")
local result = handle:read("*a")
handle:close()
if result then
local volumeMatch = string.match(result, "Playback %d+ \\[([%d]+)%%\\]")
if volumeMatch then
return tonumber(volumeMatch)
else
vlc.msg.err("[Volume Extension] Could not parse volume from amixer output. Result: " .. result) -- Debugging
return nil
end
else
vlc.msg.err("[Volume Extension] Error getting volume from amixer.")
return nil
end
end,
httpHandlers = {
set_volume = function(request)
local volume = request.query_params.volume or request.form.volume
if volume then
volume = tonumber(volume)
if volume and volume >= 0 and volume <= 100 then
if volume_extension.set_system_volume(volume) then
return { status = 200, body = "Volume set: " .. volume .. "%" }
else
return { status = 500, body = "Error setting volume" }
end
else
return { status = 400, body = "Invalid volume value (0-100)" }
end
else
return { status = 400, body = "Missing volume parameter" }
end
end,
get_volume = function(request)
local volume = volume_extension.get_system_volume()
if volume then
return { status = 200, body = tostring(volume) }
else
return { status = 500, body = "Error getting volume" }
end
end
}
}
function activate()
vlc.msg.info("[Volume Extension] Activated")
-- Register HTTP handlers directly:
vlc.http.add_handler("/set_volume", volume_extension.httpHandlers.set_volume)
vlc.http.add_handler("/get_volume", volume_extension.httpHandlers.get_volume)
end
function deactivate()
vlc.msg.info("[Volume Extension] Deactivated")
vlc.http.remove_handler("/set_volume")
vlc.http.remove_handler("/get_volume")
end
function close()
vlc.deactivate()
end
