The script you’re looking for is probably this: https://github.com/pimoroni/phat-beat/blob/master/projects/vlc-radio/phatbeatd/usr/bin/phatbeatd
It’s best left as-is, though, since it’s a fairly obtuse script not meant to be an editable example.
The relevant parts to understand are the socket interface for controlling VLC, which you can build into your script. Importing the phatbeat
library wont work, since that’s just for reading buttons and doesn’t provide control over any of the software on your Pi.
import socket
import re
VLC_HOST = "127.0.0.1"
VLC_PORT = 9294
class VLC():
def __init__(self, host="127.0.0.1", port=9294):
self.host = host
self.port = port
self.current_stream = None
self.current_state = None
self.connecting = False
self.socket = None
def send(self, command):
if self.connecting:
return False
print("Sending command: {}".format(command))
command_string = command + "\n"
if sys.version_info[0] >= 3:
command_string = command_string.encode("utf-8")
try:
self.socket.send(command_string)
except socket.error:
print("Failed to send command to VLC")
if self.connect():
self.send(command)
def recv(self, length):
value = self.socket.recv(8192)
if sys.version_info[0] >= 3:
value = value.decode("utf-8")
return value
def communicate(self, command, response_length=8192):
self.send(command)
return self.recv(response_length)
def get_current_stream(self):
self.send("status")
status = self.recv(8192)
result = re.search("input:\ (.*)\ ", status)
state = re.search("state\ (.*)\ ", status)
if state is not None:
self.current_state = state.group(1)
if result is not None:
self.current_stream = result.group(1)
else:
self.current_stream = None
return self.current_stream
def connect(self):
if self.connecting:
return
self.connecting = True
if self.socket is not None:
self.socket.close()
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
for attempt in range(10):
try:
print("Attempting to connect to VLC on {host}:{port}".format(host=self.host, port=self.port))
self.socket.connect((self.host, self.port))
print("Connection successful!")
self.connecting = False
return True
except socket.error:
time.sleep(1)
print("Connection failed!")
self.connecting = False
return False
vlc = VLC(host=VLC_HOST, port=VLC_PORT)
if vlc.connect():
# Your setup code here
And once all of the above has set up a connection to VLC, you can use the following commands to control it:
vlc.communicate("voldown") # Turn the volume down one step
vlc.communicate("volup") # Turn the volume up one step
vlc.communicate("next") # Next track
vlc.communicate("prev") # Prev track
vlc.communicate("pause") # Pause/Play
Note: To paste indented code, surround it with three backticks:
```
Like so!
```