(06-10-2025, 04:12 PM)the_bertrum Wrote: You could try the "toggle" command that flips between play and pause. Alternatively map the button to a script that checks the status and stops or plays accordingly.
from gpiozero import Button
from signal import pause
import socket
import json
# GPIO17 = pin fisico 11
button = Button(17)
# Path del socket creato da MPV
MPV_SOCKET = "/tmp/mpvsocket"
def toggle_play_pause():
try:
client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
client.connect(MPV_SOCKET)
# MPV usa JSON per i comandi
command = {"command": ["cycle", "pause"]}
client.send((json.dumps(command) + "\n").encode("utf-8"))
client.close()
except Exception as e:
print(f"Errore nel contattare MPV: {e}")
# Collega la funzione alla pressione del bottone
button.when_pressed = toggle_play_pause
This is my
python3 toggle_mpv.py
Da provare