Button shim for internet radio station selection (with VLC) with pHAT DAC on rpm zero

Hi
Still kind of new to Python, but I thought I better start with a project to learn it.

I wanted to make a combined internet radio with the possibility to stream with airplay (got that working) and to have easy selection of internet radio stations with buttons (not yet working). I have a RPi 0 with a button shim and a pHAT DAC.

With inspiration from another project involving thet button shim, I tried with the python code below.
When I run the script and presses button A, the stream starts playing. When I press button B, the other stream plays simultaneously. How to make the previous stream stop and player the other instead?

import time
import signal
import buttonshim
import os
import vlc

def flash_led(interval, times, r, g, b):
for i in range(times):
    buttonshim.set_pixel(r, g, b)
    time.sleep(interval)
    buttonshim.set_pixel(0, 0, 0)
    time.sleep(interval)

def buttonflash():
flash_led(0.025, 3, 255, 255, 255) 

# Button A
@buttonshim.on_release(buttonshim.BUTTON_A)
def button_a(button, pressed):
buttonflash()
os.system('cvlc http://live-icy.gss.dr.dk/A/A03H.mp3.m3u')

# Button B
@buttonshim.on_release(buttonshim.BUTTON_B)
def button_a(button, pressed):
buttonflash()
os.system('cvlc http://live-icy.gss.dr.dk/A/A04H.mp3.m3u')

One of these might help, worth a look see anyway.

https://learn.pimoroni.com/tutorial/sandyj/streaming-spotify-to-your-pi

https://learn.pimoroni.com/tutorial/sandyj/streaming-airplay-to-your-pi

I found them here

I have the pHat Beat setup as an Internet radio. Clicking the buttons on it will do what you want, sort of kind of. Pressing a button moves to the next or previous channel in the m3u file. What was playing stops and the new channel starts. You need to stop the stream thats playing, before starting a new one. I don’t know what code to enter to do that though?

Thanks! I thought of doing it like that, but those projects use a “skip” function to change to the next track. My idea was to have one station set for each of the buttons on the shim.

I like that idea, it is likely doable, I don’t have those skills though. The following may help or just confuse lol but here goes. This is a list of VLC radio commands I clipped from another thread a long time ago.

pi@raspberry:~ $ vlc -I rc
VLC media player 2.2.5 Weatherwax (revision 2.2.5-0-g9275f0fefa)
Command Line Interface initialized. Type `help’ for help.
longhelp

±—[ CLI commands ]
| add XYZ . . . . . . . . . . . . . . . . . . . . add XYZ to playlist
| enqueue XYZ . . . . . . . . . . . . . . . . . queue XYZ to playlist
| playlist . . . . . . . . . . . . . show items currently in playlist
| search [string] . . search for items in playlist (or reset search)
| delete . . . . . . . . . . . . . . . . delete item X in playlist
| move [Y] . . . . . . . . . . . . move item X in playlist after Y
| sort key . . . . . . . . . . . . . . . . . . . . . sort the playlist
| sd [sd] . . . . . . . . . . . . . show services discovery or toggle
| play . . . . . . . . . . . . . . . . . . . . . . . . . . play stream
| stop . . . . . . . . . . . . . . . . . . . . . . . . . . stop stream
| next . . . . . . . . . . . . . . . . . . . . . . next playlist item
| prev . . . . . . . . . . . . . . . . . . . . previous playlist item
| goto, gotoitem . . . . . . . . . . . . . . . . . goto item at index
| repeat [on|off] . . . . . . . . . . . . . . toggle playlist repeat
| loop [on|off] . . . . . . . . . . . . . . . . toggle playlist loop
| random [on|off] . . . . . . . . . . . . . . toggle playlist random
| clear . . . . . . . . . . . . . . . . . . . . . clear the playlist
| status . . . . . . . . . . . . . . . . . . . current playlist status
| title . . . . . . . . . . . . . . set/get title in current item
| title_n . . . . . . . . . . . . . . . . next title in current item
| title_p . . . . . . . . . . . . . . previous title in current item
| chapter . . . . . . . . . . . . set/get chapter in current item
| chapter_n . . . . . . . . . . . . . . next chapter in current item
| chapter_p . . . . . . . . . . . . previous chapter in current item
|
| seek X . . . . . . . . . . . seek in seconds, for instance `seek 12’
| pause . . . . . . . . . . . . . . . . . . . . . . . . toggle pause
| fastforward . . . . . . . . . . . . . . . . . . set to maximum rate
| rewind . . . . . . . . . . . . . . . . . . . . . set to minimum rate
| faster . . . . . . . . . . . . . . . . . . faster playing of stream
| slower . . . . . . . . . . . . . . . . . . slower playing of stream
| normal . . . . . . . . . . . . . . . . . . normal playing of stream
| rate [playback rate] . . . . . . . . . . set playback rate to value
| frame . . . . . . . . . . . . . . . . . . . . . play frame by frame
| fullscreen, f, F [on|off] . . . . . . . . . . . . toggle fullscreen
| info . . . . . . . . . . . . . information about the current stream
| stats . . . . . . . . . . . . . . . . show statistical information
| get_time . . . . . . . . . seconds elapsed since stream’s beginning
| is_playing . . . . . . . . . . . . 1 if a stream plays, 0 otherwise
| get_title . . . . . . . . . . . . . the title of the current stream
| get_length . . . . . . . . . . . . the length of the current stream
|
| volume . . . . . . . . . . . . . . . . . . set/get audio volume
| volup . . . . . . . . . . . . . . . raise audio volume X steps
| voldown . . . . . . . . . . . . . . lower audio volume X steps
| achan . . . . . . . . . . . . set/get stereo audio output mode
| atrack . . . . . . . . . . . . . . . . . . . set/get audio track
| vtrack . . . . . . . . . . . . . . . . . . . set/get video track
| vratio . . . . . . . . . . . . . . . set/get video aspect ratio
| vcrop, crop . . . . . . . . . . . . . . . . set/get video crop
| vzoom, zoom . . . . . . . . . . . . . . . . set/get video zoom
| vdeinterlace . . . . . . . . . . . . . set/get video deinterlace
| vdeinterlace_mode . . . . . . . set/get video deinterlace mode
| snapshot . . . . . . . . . . . . . . . . . . . . take video snapshot
| strack . . . . . . . . . . . . . . . . . set/get subtitle track
| hotkey, key [hotkey name] . . . . . . . . . . simulate hotkey press
|
| vlm . . . . . . . . . . . . . . . . . . . . . . . . . load the VLM
| set [var [value]] . . . . . . . . . . . . . . . . . set/get env var
| save_env . . . . . . . . . . . . save env vars (for future clients)
| alias [cmd] . . . . . . . . . . . . . . . . set/get command aliases
| description . . . . . . . . . . . . . . . . . describe this module
| license . . . . . . . . . . . . . . . . print VLC’s license message
| help, ? [pattern] . . . . . . . . . . . . . . . . . a help message
| longhelp [pattern] . . . . . . . . . . . . . . a longer help message
| lock . . . . . . . . . . . . . . . . . . . . lock the telnet prompt
| logout . . . . . . . . . . . . . . exit (if in a socket connection)
| quit . . . . . . . . quit VLC (or logout if in a socket connection)
| shutdown . . . . . . . . . . . . . . . . . . . . . . . shutdown VLC
±—[ end of help ]

Thanks. I’ll have a try and let you know when/if it works.