Skip to content

Commit 556e962

Browse files
authored
Internet radio
Add internet radio support (still need to move radio stations to the config file). Also added bounce time to right button, because mine started wearing off (also need to place in the config)
1 parent 74bc33a commit 556e962

File tree

1 file changed

+110
-3
lines changed

1 file changed

+110
-3
lines changed

boot.py

Lines changed: 110 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# A runatboot file
12
import vlc #play music
23
import time #control time
34
import discid #read discid
@@ -50,7 +51,7 @@
5051
lcd.blink = False
5152

5253
Button_up = Button(config.get('Buttons', 'Up'))
53-
Button_right = Button(config.get('Buttons', 'Right'))
54+
Button_right = Button(config.get('Buttons', 'Right'),bounce_time=0.05)
5455
Button_left = Button(config.get('Buttons', 'Left'))
5556
Button_middle = Button(config.get('Buttons', 'Middle'))
5657
Button_down = Button(config.get('Buttons', 'Down'))
@@ -59,7 +60,7 @@
5960
command = 0 # 0-nothing 1-play 2-pause 3-stop 4-next 5-prev
6061
textb = 15
6162
page = 0
62-
MAX_PAGE = 1
63+
MAX_PAGE = 2
6364
start = 0
6465

6566
playchar = [
@@ -236,6 +237,70 @@ def PlayCd(conn):
236237
print("Scrobbling failed: " + artists[index] + track_list[index] + album + str(time.time()))
237238
print("Reason? ->")
238239
print(e)
240+
241+
def loop_media(event):
242+
print("Station ended. Moving to next station...")
243+
listplayer.next()
244+
245+
def PlayRadio(conn):
246+
instance = vlc.Instance()
247+
player = instance.media_player_new()
248+
listplayer = instance.media_list_player_new()
249+
listplayer.set_media_player(player)
250+
251+
# Create a playlist
252+
playlist = instance.media_list_new()
253+
254+
radio_stations = {
255+
"RP Main mix": "http://stream.radioparadise.com/flacm",
256+
"RP Mellow mix": "http://stream.radioparadise.com/mellow-flacm",
257+
"RP Rock mix": "http://stream.radioparadise.com/rock-flacm",
258+
"RP Global mix": "http://stream.radioparadise.com/global-flacm",
259+
"Radio 357": "https://stream.radio357.pl",
260+
"Tok Fm": "http://radiostream.pl/tuba10-1.mp3",
261+
"Rock Radio": "http://radiostream.pl/tuba8-1.mp3"
262+
}
263+
i_tracks = len(radio_stations)
264+
# Add streams to the playlist
265+
for station_name, stream_url in radio_stations.items():
266+
media = instance.media_new(stream_url)
267+
media.set_meta(vlc.Meta.Title, station_name)
268+
playlist.add_media(media)
269+
270+
# Set the media list to the player
271+
listplayer.set_media_list(playlist)
272+
273+
# Register the callback
274+
event_manager = player.event_manager()
275+
event_manager.event_attach(vlc.EventType.MediaPlayerEndReached, loop_media)
276+
277+
# Start playing the first item in the playlist
278+
print("Playing Radio Paradise streams...")
279+
listplayer.play()
280+
while True:
281+
dump = conn.recv()
282+
index = playlist.index_of_item(listplayer.get_media_player().get_media())
283+
current_media = player.get_media()
284+
match dump:
285+
case 0:
286+
conn.send([index+1, i_tracks, list(radio_stations.keys())[index], current_media.get_meta(vlc.Meta.Artist),current_media.get_meta(vlc.Meta.Album), MsToTime(player.get_time(), player.get_length()), player.get_state()])
287+
#case 1:
288+
#listplayer.play()
289+
case 2:
290+
if(player.get_state() == vlc.State.Stopped):
291+
listplayer.play()
292+
else:
293+
listplayer.pause()
294+
case 3:
295+
listplayer.stop()
296+
#last_scrobble=""
297+
case 4:
298+
listplayer.next()
299+
#last_scrobble=""
300+
case 5:
301+
listplayer.previous()
302+
303+
239304
def ShowCd(dump):
240305
global textb
241306
lcd.clear()
@@ -285,6 +350,11 @@ def next():
285350
command = 4
286351
else:
287352
parent_conn.send(4)
353+
case 2:
354+
if(not run):
355+
command = 4
356+
else:
357+
parent_conn.send(4)
288358
case 1:
289359

290360
command = 4
@@ -296,6 +366,11 @@ def back():
296366
command = 5
297367
else:
298368
parent_conn.send(5)
369+
case 2:
370+
if(not run):
371+
command = 5
372+
else:
373+
parent_conn.send(5)
299374
case 1:
300375

301376
command = 5
@@ -304,13 +379,17 @@ def stop():
304379
match page:
305380
case 0:
306381
parent_conn.send(3)
382+
case 2:
383+
parent_conn.send(3)
307384
case 1:
308385
command = 3
309386
def pause():
310387
global run, command
311388
match page:
312389
case 0:
313390
parent_conn.send(2)
391+
case 2:
392+
parent_conn.send(2)
314393
case 1:
315394
command = 2
316395
def middle():
@@ -410,5 +489,33 @@ def shutdown():
410489
run=False
411490
command = 0
412491
time.sleep(1)
492+
case 2:
493+
lcd.clear()
494+
lcd.message = "Internet\nRadio"
495+
if(run):
496+
lcd.clear()
497+
lcd.message = "Loading..."
498+
cdplayer = Process(target=PlayRadio, args=(child_conn,))
499+
cdplayer.start()
500+
time.sleep(2)
501+
while(run):
502+
if command == -1 and cdplayer.is_alive():
503+
cdplayer.kill()
504+
cdplayer.join()
505+
run = False
506+
elif cdplayer.is_alive():
507+
parent_conn.send(0)#you must send to recive
508+
time.sleep(0.1)
509+
dump=parent_conn.recv()
510+
if(dump[6] == vlc.State.Ended):
511+
cdplayer.kill()
512+
cdplayer.join()
513+
lcd.clear()
514+
run = False
515+
else:
516+
ShowCd(dump)
517+
else:
518+
run = False
519+
command=0
520+
time.sleep(1)
413521
time.sleep(0.5)
414-

0 commit comments

Comments
 (0)