car_ui/audio/system_volume.py
2026-01-09 02:57:15 +03:00

25 lines
571 B
Python

import shutil
import subprocess
def set_volume(value: int):
value = max(0, min(100, value))
if shutil.which("wpctl"):
_run_cmd(["wpctl", "set-volume", "@DEFAULT_AUDIO_SINK@", f"{value}%"])
return
if shutil.which("amixer"):
_run_cmd(["amixer", "sset", "PCM", f"{value}%"])
def _run_cmd(args: list[str]):
try:
subprocess.run(
args,
capture_output=True,
text=True,
timeout=1,
check=False,
)
except (OSError, subprocess.SubprocessError):
pass