Streaming internet radio on the Raspberry Pi

Streaming Internet Radio on the Raspberry Pi
Published: by
Categories
Raspberry Pi

So as part of my home automation system, the idea was to use a Raspberry Pi connected to the internet to play an online / internet radio stream.

Introduction

This post will explain how I managed to stream internet radio on the Raspberry Pi using the terminal or a Bash Script. This process can also be done through PuTTY.

This is actually a nice first-time project for new Raspberry Pi owners.

I will be using Radio Sonder Grense (RSG), our local South African Afrikaanse radio station (and frankly the best radio station in the world), now gone globally via internet streaming.

Background

We will be using Mplayer to play internet radio. While some media players may need PLS or M3U files, Raspbian’s Mplayer will need ‘direct stream links’ (a link to the actual streaming server) or ASX files (advance stream director files which stores playlists).

Using PLS, ASX or M3U links is apparently the best, as these links are usually permanent and will not change. Direct stream links can vary based on individual server availability. If a direct server link stops working, you’ll need to get the updated links from the streaming website. I will attempt to keep them updated here.

Streaming internet radio on the Raspberry Pi

Assumptions / you will need

A fully set up Raspberry Pi running the latest update of Raspbian connected to the internet via an Ethernet cable. Will need a keyboard and screen output or PuTTY to see what you’re doing. I’m also going to assume that your audio is playing to your desired speaker output. In this example, you can use the streaming services of RSG and Antfarm Internet Radio, but you will obviously need a similar service of your favourite radio station.

Prior steps

Audio player software

For this project, we will be using MPlayer to stream internet radio on the Raspberry Pi. MPlayer is a free, open-source video and audio player for all major operating systems. It can play various file formats including .mp3 and audio streams.

Raspbian does not come with MPlayer pre-installed, so you can do the installation with:

sudo apt-get install mplayer

Out of the box, MPlayer will work just fine for audio streaming. It works simply by using mplayer in the command line:

Output can be tested by the the following command:

mplayer /usr/share/scratch/Media/Sounds/Vocals/Singer2.wav

This tells MPlayer to play the pathed .wav file through your default audio hardware settings. Like with some other ALSA friendly audio players, the audio hardware can also be set by using the following:

mplayer -ao alsa:device=hw=0,0 /usr/share/scratch/Media/Sounds/Vocals/Singer2.wav

or

mplayer -ao alsa:device=default /usr/share/scratch/Media/Sounds/Vocals/Singer2.wav

On the RSG home page, the following links are available under different devices:

  • http://radio.antfarm.co.za/cdn/clients/sabc/rsg/rsg_22.asx
  • http://radio.antfarm.co.za/cdn/clients/sabc/RSG/mplayer.asp
  • http://46.4.25.213:9032/;listen.mp3
  • http://radio.antfarm.co.za/cdn/clients/sabc/RSG/listen.plsv
  • rtsp://216.246.37.52:1935/rsg/RSG-Powered_by_Antfarm.stream
  • http://radio.antfarm.co.za/cdn/clients/sabc/RSG/player.asp?id=65

From this list you can see that there are two direct stream links (.mp3 & .stream) and one ASX file. The .mp3 link worked for me. To use a stream link:

mplayer http://46.4.25.213:9032/;listen.mp3

or

mplayer -ao alsa:device=hw=0,0 http://46.4.25.213:9032/;listen.mp3

to work with an ALSA device.

While the stream is playing, you can use p on your keyboard to pause, m to mute and q to quit the stream playback.

Update: Just for interest sake, you can also stop MPlayer from an external logged in terminal by using the sudo pkill -f mplayer command.

Playlists

MPlayer also has a nice playlist function, which we won’t be going into much detail now, but you might need to use it if the normal commands does not work. What I’ve found is that you have to use MPlayer‘s -playlist function for ASX files. The address the becomes quoted for example:

mplayer -playlist "http://radio.antfarm.co.za/cdn/clients/sabc/rsg/rsg_22.asx"

Although I personally couldn’t get it to work, it seems like playlisting is also needed for ASP links — maybe you come across other internet radio links that might need it.

Other cool radio station links

DEF CON Radio FM
mplayer http://xstream1.somafm.com:6200
or see http://somafm.com/defcon/directstreamlinks.html for their updated list.

Radio Landsberg International
mplayer http://80.237.154.83:8120
or see http://www.radiolandsberg.international for their website.

KDFC (California Classical)
mplayer http://8343.live.streamtheworld.com:80/KDFCFMAAC_SC
or see http://www.kdfc.com/pages/15744898.php for their updated list.

Classic FM
mplayer -playlist "http://radio.antfarm.co.za/cdn/clients/classic/classicfm_22.asx"
or see http://www.classicfm.co.za for their website.

Conclusion

This post showed how to stream audio with the Raspberry Pi.
Please share your favourite radio station’s streaming address in the comment section below.

About the author
Renier busies himself with improving his English writing, creative web design and his websites, photoshopping, micro-electronics, multiple genres of music, superhero movies and badass series.
Behind the Scenes is a free, informative website. If you find value in any of our content, please consider making a donation to our cause, or becoming a Patron for exclusive content.
Donate via PayPal Become a Patron

Save, share & discuss

Your comment is important, but don't be a knob. Keep it constructive and polite.

4 thoughts on “Streaming internet radio on the Raspberry Pi”

  1. Addition: mplayer does seem to have problems with some streams. I found out you can also use VLC without GUI / interface by using cvlc so in the case of the above it would be something like:

    cvlc://8343.live.streamtheworld.com:80/KDFCFMAAC_SC

    Advantages? Playing tends to start quicker than mplayer and more formats of streams seem to be supported. Reminder: If you start a stream using SSH the stream stops if you break the SSH-connection so use VNC to start a stream then close VNC and it keeps playing fine. Again thanks for the idea / tips

  2. I have been looking for this solution for a while to make an old Raspi-2B into a radiostream-player and it WORKS! Thank you so much for your explanation.

Leave a Reply

Your email address will not be published. Required fields are marked *

More Raspberry Pi related posts