Skip to content

Instantly share code, notes, and snippets.

@ednisley
Created February 23, 2016 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ednisley/8cbc30627015873715ea to your computer and use it in GitHub Desktop.
Save ednisley/8cbc30627015873715ea to your computer and use it in GitHub Desktop.
Python source code: Raspberry Pi Streaming Radio Player
from evdev import InputDevice,ecodes,KeyEvent
import subprocess32
import select
import re
import sys
Media = {'KEY_KP7' : ['Classical',['mplayer','-playlist','http://stream2137.init7.net/listen.pls']],
'KEY_KP8' : ['Jazz',['mplayer','-playlist','http://stream2138.init7.net/listen.pls']],
'KEY_KP9' : ['WMHT',['mplayer','http://live.str3am.com:2070/wmht1']],
'KEY_KP4' : ['Dub 1',['mplayer','-playlist','http://dir.xiph.org/listen/2645/listen.m3u']],
'KEY_KP5' : ['Dub 2',['mplayer','http://streaming207.radionomy.com:80/MiamiClubMusiccom']],
'KEY_KP6' : ['WAMC',['mplayer','http://pubint.ic.llnwd.net/stream/pubint_wamc']],
'KEY_KP1' : ['Oldies 1',['mplayer','http://streaming304.radionomy.com:80/keepfree60s']],
'KEY_KP2' : ['Oldies 2',['mplayer','http://streaming207.radionomy.com:80/1000Oldies']],
'KEY_KP3' : ['Soft Rock',['mplayer','http://streaming201.radionomy.com:80/SoftRockRadio']],
'KEY_KP0' : ['Smooth',['mplayer','http://streaming202.radionomy.com:80/The-Smooth-Lounge']]
}
CurrentKC = 'KEY_KP7'
Controls = {'KEY_KPSLASH' : '/',
'KEY_KPASTERISK' : '*',
'KEY_KPENTER' : ' ',
'KEY_KPMINUS' : '<',
'KEY_KPPLUS' : '>'
}
# set up event input and polling
k=InputDevice('/dev/input/keypad')
kp = select.poll()
kp.register(k.fileno(),select.POLLIN + select.POLLPRI + select.POLLERR)
# set up files for mplayer pipes
lw = open('/tmp/mp.log','w') # mplayer piped output
lr = open('/tmp/mp.log','r') # ... reading that output
# Start the default stream
print 'Starting mplayer on',Media[CurrentKC][0]," -> ",Media[CurrentKC][-1][-1]
p = subprocess32.Popen(Media[CurrentKC][-1],stdin=subprocess32.PIPE,stdout=lw,stderr=subprocess32.STDOUT)
print ' ... running'
#--- Play the streams
while True:
# pluck next line from mplayer and decode it
text = lr.readline()
if 'ICY Info: ' in text:
trkinfo = text.split(';')
for ln in trkinfo:
if 'StreamTitle' in ln:
trkhit = re.search(r"StreamTitle='(.*)'",ln)
TrackName = trkhit.group(1)
print 'Track name: ', TrackName
break
elif 'Exiting...' in text:
print 'Got EOF / stream cutoff'
print ' ... killing dead mplayer'
p.kill()
print ' ... flushing pipes'
lw.truncate(0)
print ' ... discarding keys'
while [] != kp.poll(0):
kev = k.read
print ' ... restarting mplayer: ',Media[CurrentKC][0]
p = subprocess32.Popen(Media[CurrentKC][-1],stdin=subprocess32.PIPE,stdout=lw,stderr=subprocess32.STDOUT)
print ' ... running'
continue
# accept pending events from keypad
if [] != kp.poll(0):
kev = k.read()
for e in kev:
if e.type == ecodes.EV_KEY:
kc = KeyEvent(e).keycode
if kc == 'KEY_NUMLOCK':
continue
# print 'Got: ',kc
if (kc == 'KEY_BACKSPACE') and (KeyEvent(e).keystate == KeyEvent.key_hold):
if True:
print 'Backspace = shutdown!'
p = subprocess32.call(['sudo','shutdown','-HP',"now"])
else:
print 'BS = bail from main, ssh to restart!'
sys.exit(0)
if KeyEvent(e).keystate != KeyEvent.key_down:
continue
if kc in Controls:
print 'Control:', kc
try:
p.stdin.write(Controls[kc])
except Exception as e:
print "Can't send control: ",e
print ' ... restarting player: ',Media[CurrentKC][0]
p = subprocess32.Popen(Media[CurrentKC][-1],stdin=subprocess32.PIPE,stdout=lw,stderr=subprocess32.STDOUT)
print ' ... running'
if kc in Media:
print 'Switching stream to ',Media[kc][0]," -> ",Media[kc][-1][-1]
CurrentKC = kc
print ' ... halting player'
try:
p.communicate(input='q')
except Exception as e:
print 'Perhaps mplayer died?',e
print ' ... killing it for sure'
p.kill()
print ' ... flushing pipes'
lw.truncate(0)
print ' ... restarting player: ',Media[CurrentKC][0]
p = subprocess32.Popen(Media[CurrentKC][-1],stdin=subprocess32.PIPE,stdout=lw,stderr=subprocess32.STDOUT)
print ' ... running'
print 'Out of loop!'
@ednisley
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment