A beautiful web radio for a living room music system

Totally Happy?

The solution outlined above has some disadvantages. First, portals look to earn money and therefore regularly treat users to advertising spots. Second, many stations also transmit interesting additional data about the tracks and artists, which a plain vanilla music player does not reproduce (Figure 9). Thirdly, the portals usually offer only very limited options for recording their programs.

Figure 9: Example text info supplementing a radio broadcast.

In principle, the solution consists of its own collection of links that bypasses portal and station websites and taps directly into the music source. Everything else about program logic is only for convenient control and navigation, as well as the appealing display.

A little research on the web turns up a number of web radios for the Raspberry Pi. The most comprehensive collection is provided by a web radio connoisseur named Bob Rathbone [7]. Besides the software, the project provides a 340-page document that explains the installation and configuration of various hardware and software components on the Raspberry Pi down to the minutest detail. Even if you have never owned an SBC like this, you can build a web radio without any problems, provided you separate the wheat from the chaff, which is not so easy.

However, Rathbone's solution relies on difficult-to-install software with a less than appealing interface, which ultimately pushed my decision in favor of designing a DIY solution.


For an initial test you will want to use a new, freshly installed Raspberry Pi OS – make sure you have the latest version (at the editorial deadline, this was "Buster"). A Pi Zero with the Lite version is perfectly okay for the project, unless you want an additional screen for the browser, as in the living room radio presented here. You also need the normal desktop version of the operating system and at least a second-generation Raspberry Pi, preferably later, because the graphical interface on the earlier versions is a little rough.

The installation commands are as follows:

$ sudo apt-get -y remove pulseaudio
$ git clone https://github.com/bablokb/pi-webradio.git
$ cd pi-webradio
$ sudo tools/install pi
$ sudo systemctl enable pi-webradio.service
$ sudo systemctl start pi-webradio.service

You only need the first call for desktop systems, because PulseAudio, which is installed there by default, does not support audio output from a background service. If the default user on your system is not named pi, you also need to add the appropriate user to the installation command (command 4), who needs to be a member of the audio group and have sudo rights. After the install, activate and start the service (last two commands). After doing so, you can access the web controls in a browser.

To get a landscape aspect, I optimized the responsive interface. On typical desktops, smartphones, and tablets, the system can be operated in portrait format without any problems. By the way, the web server built into radio software delivers exactly one web page to the browser once only. After that, the server and browser only exchange small blocks of data; the web browser takes over the display completely, ensuring that the server part of the software will run without problems, even on SBCs like the Pi Zero. A few details relating to the technical implementation can be found in the "Under the Hood" box.

Under the Hood

The Pi web radio software is based on Python 3 and uses the mpg123 command-line tool to play the audio data. The program provides a remote control mode with the -R option. You can easily try this out by calling

mpg123 -R

at the command line and then typing h – the program will then display all commands.

The web server used here is Flask, a micro-framework that goes about its task in many Python application servers. The web radio uses only a few of its functions, because the web server just passes API calls like radio_play_channel to the appropriate Python class, of which there is only one per task (e.g., Radio, Player, or Recorder). Some auxiliary classes are available, as well.

The web interface uses CSS3 and JavaScript; jQuery acts as an auxiliary library. The application doesn't go all out to support all possible browsers, but today's crop of web browsers all offer what it takes.


By default, the tool already supports a number of German stations, but for copyright reasons you do not get to see the station logos on launch (Figure 10). The broadcasters probably don't care, but dubious lawyers take every opportunity to earn money, and I don't want to give them any reason to sue me. It takes some configuration work to make the web radio serve your musical tastes and also present the station selection in a visually appealing way.

Figure 10: Channel selection: Note that you can localize your station selection in the software.

The channel list itself can be found in the /etc/pi-webradio.channels file. It stores the name, a URL, and a logo (without path) for each channel. After clicking on the desired station, you can download and save the logos by right-clicking. The files in JPEG or PNG format belong in the /usr/local/lib/pi-webradio/web/images/ directory; you then need to enter the names of the logo files in the station list.

You need a bit more detective work for a broadcaster's URL, because not all broadcasters publish them, and those that do obfuscate the info as well as they can on their websites. The IT horizon of some broadcasters obviously does not go beyond their own web presence. If you can't find the URL despite an intensive search, you can still use the developer tools in your browser. First, navigate to the radio station page that supports live listening, but do not click the play button.

First launch the developer tools from the menu or by pressing F12. Another window opens, in which you navigate to the Network tab. Then press the live play button. The page now fills with all the URLs that the browser retrieves (Figure 11).

Figure 11: The developer tools in the browser reveal the URL for the media stream.

When you get there, search for a URL containing mp3, m3u, stream, icecast, or shoutcast. The list is not exhaustive, but almost always takes you to the target. Copy the URL and start the playback with mpg123 <URL>. The mpg123 playback program does not handle encryption, so you need to replace a leading https in the URL with http up front. If you enter the correct URL, you should now hear the station playing. Press Q to cancel the playback.

Besides the /etc/pi-webradio.channels file, in the /etc/pi-webradio.conf configuration file you can change the port for the web server (default=8026), for example, or specify the directory in which you will be storing any recordings.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Nyttig

    Turn a Raspberry Pi into a useful personal micro server for streaming Internet radio, reading RSS feeds, jotting down notes, sharing files, and more.

  • FSFE and FFII to Radio Station Winners: "rOGG on"

    Radio Free Deutschland: For Document Freedom Day, March 31 2010, a couple of European radio stations were granted awards for using open standards.

  • Pi FM Radio

    Low-cost RTL-SDR dongles can read frequencies between 24 and 1,766MHz. We built a simple FM radio with a Raspberry Pi, a USB dongle based on the RTL2832U chipset, an LCD HAT, and some Python code.

  • Last.fm

    Last.fm tracks your musical tastes and helps you find users with similar interests.

  • Internet Radio Intro

    Did you think the Internet was out of revolutions? With Internet radio, anyone can be a broadcaster. If you want to start broadcasting or podcasting, or even if you're just looking for the perfect station, check out this month's Internet Radio cover story.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More