Wednesday, 20 February 2013

Raspberry Pi Radio Streamer

Another use I've found for my Raspberry Pi is to use it to stream music from the BBC and also  http://somafm.com. I'm using the command line rather than the GUI so I can't easily browse to the site and launch the stream through the media player. Here's the code:


 #! /bin/bash  
 clear  
 TODAY=$(date)  
 HOST=$(hostname)  
 echo "-----------------------------------------------------"  
 echo "$HOST       $TODAY"  
 echo "                           "  
 echo "Welcome to the Raspberry Pi Radio Streamer      "  
 echo "-----------------------------------------------------"  
 echo "Hi, $USER!"  
 echo "Which station would you like to listen to?"  
 echo "1) BBC Radio 1"  
 echo "2) BBC Radio 2"  
 echo "3) BBC Radio 3"  
 echo "4) BBC Radio 4"  
 echo "5) BBC Radio 5 Live"  
 echo "6) BBC 6 Music"  
 echo "7) Jazz FM"  
 echo "8) Smooth 70s"  
 echo "9) Birdsong Radio"  
 echo "20) SomaFM - Mission Control"  
 read n  
 case $n in  
     1) echo "Playing BBC Radio 1" && mplayer http://www.bbc.co.uk/radio/listen/live/r1_aaclca.pls > /dev/null 2>&1 & ;;  
     2) echo "Playing BBC Radio 2" && nohup mplayer http://www.bbc.co.uk/radio/listen/live/r2_aaclca.pls > /dev/null 2>&1 & ;;  
     3) echo "Playing BBC Radio 3" && mplayer http://www.bbc.co.uk/radio/listen/live/r3_aaclca.pls > /dev/null 2>&1 & ;;  
     4) echo "Playing BBC Radio 4" && mplayer http://www.bbc.co.uk/radio/listen/live/r4_aaclca.pls > /dev/null 2>&1 & ;;  
     5) echo "Playing BBC Radio 5 Live" && mplayer http://www.bbc.co.uk/radio/listen/live/r5l_aaclca.pls > /dev/null 2>&1 & ;;  
     6) echo "Playing BBC 6 Music" && mplayer http://www.bbc.co.uk/radio/listen/live/r6_aaclca.pls & ;;  
     7) echo "Playing Jazz FM" && mplayer http://listen.onmyradio.net:8002/ > /dev/null 2>&1 & ;;  
     8) echo "Playing Smooth 70s" && mplayer http://shoutcast.gmgradio.com:10008/ > /dev/null 2>&1 & ;;  
     9) echo "Playing Birdsong Radio" && mplayer http://stardust.wavestreamer.com:8062 > /dev/null 2>&1 & ;;  
     20) echo "Playing SomaFM - Mission Control" && mplayer http://somafm.com/missioncontrol.pls > /dev/null 2>&1 & ;;  
     *) invalid option;;  
 esac  
It's not terribly complicated, it presents a simple menu, reads user input and points mplayer towards a corresponding stream. It's designed to be portable and should work on any system that has mplayer installed. I want to try to get a "Now Playing" message added to it as the next step. I suppose I need to get it to read the BBC website and scrape it from there somehow and echo it back to the screen. I also need to work out a better way of stopping it playing other than "killall mplayer" when I need to shut it off.

Feel free to take the code and modify it, it's very rough and could use more features. It's very simple but it does the job well enough for my needs.

1 comment:

  1. Great script, thanks going to get this set up on my pi too.

    Would like to see the "Now Playing" too, if you find a way =)

    ReplyDelete