Thank you for your donation!


Cloudsmith graciously provides open-source package management and distribution for our project.


Problem: GPIO Buttons
#5
(06-11-2025, 08:46 AM)nadirfly Wrote:
(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

Hi mate! Mind me if you can, share and do instruction to work with buttons i have harware momentary here. I just want to use these button to run. And can you share the code and it is necessary to add a command on peripheral gpio button command after importing a code. 
Button 1: play/pause gpio 22
Button 2: shutdown gpio 4
Button 3: next
Button 4: prev

Thank you
Reply


Messages In This Thread
GPIO Buttons - by nadirfly - 06-10-2025, 03:19 PM
RE: GPIO Buttons - by the_bertrum - 06-10-2025, 04:12 PM
RE: GPIO Buttons - by nadirfly - 06-11-2025, 08:46 AM
RE: GPIO Buttons - by the_bertrum - 06-11-2025, 01:55 PM
RE: GPIO Buttons - by kuyadreiph - 06-24-2025, 03:49 AM

Forum Jump: