In the first part, we used MPlayer to play audio files and stream from Internet radio. We wrote a program to allow for remote control. It would be better if there were an easier way though. This is why this time, we’re going to control the radio through a smartphone app!
We’ll be following these steps to connect Raspberry Pi with a smartphone.
Preparing Raspberry Pi
While it’s possible to install an OS dedicated to play media, we want to keep Raspbian installed for now. So, we’ll be doing this by using the MPD (Music Player Daemon) package.
Music Player Daemon – Wikipedia
Music Player Daemon (MPD) is a free and open source music player server. It plays audio files, organizes playlists and maintains a music database. In order to interact with it, a separate client is needed.
Let’s install those two packages for the MPD and the MPD client!
apt-get install mpd
apt-get install mpc
The MPC package is the client for MPD. Make sure to install both of them.
After completing the installation, we’ll need to configure MPD’s settings to allow external access. While we could use the “vi” command to edit the configuration file from the command line, it’s over 400 lines long. So, it will be much easier to just edit it directly.
/etc/mpd.conf （Around line 82)
# bind_to_address "localhost"
Add a “#” at the start of the “bind_to_address “localhost”” line to turn it into a comment.
While this is not absolutely necessary, you can also set automatic playlist updates and volume control from the same mpd.conf file. Like what we just did, take the “#” out of the line to activate the settings for these.
/etc/mpd.conf （around line 121）
/etc/mpd.conf （around line 322）
When you’re done with the configuration, restart MPD from the LX Terminal.
service mpd restart
If you’ve made it this far, then you’re done setting up MPD!
Next, we’ll get to work on configuring MPC. First, let’s take a look at the command options.
That’s a lot of commands!
In this list, you can see standard audio player functions, like “repeat” and “volume”. Last time with MPlayer, we had a hard time getting the music to stop. But MPC has a “stop” command, so it’ll be a piece of cake to set up this time!
Let’s Do a Test Hearing of Our mp3 Files Before We Start on the Radio!
This is the “music_directory.” By default, it points to ” /var/lib/mpd/music/ .” This folder is where you’ll need to put your audio files. Like before, we used royalty-free MP3s for this test.
Create a folder named “test1” in your Music folder (/var/lib/mpd/music/) for your MP3. I used three MP3. Then, execute the “update” command from the LX Terminal to make a playlist from the Music folder content.
Let’s check the list of files to make sure that everything was loaded. Execute the “ls” command.
mpc ls test1
“test1” was found! The three files were shown here after using the “ls” command and designating the directory name. So, we can be sure that everything was loaded properly.
Next, we’ll use the “add” command to add them to the playlist.
mpc add test1
While we could add each file one by one, designating the directory name will add all the files in the root of the folder.
Everything is set up now, so all that’s left is to execute the “play” command!
Could you play your music?
During the playback, you can have access to the following options in the console: “volume,” “repeat,” “random,” “single,” and “consume”. Other commands can also be executed without having to stop the music.
mpc save test1_list
To save the current playlist, we need to use the “save” command. By doing so, the following file will be created in the saved playlist directory.
When you execute “save,” the playlist will be cleared. To use the same playlist again, we need to load it up again with the “load” command.
mpc load test1_list
Tune in to Internet Radio!
Once you’ve finished setting up the packages and run your tests, we can start tuning in to Internet radio! MPlayer used the playlist file URL as an argument for this. But MPC will need you to download a playlist file. (It uses the URLs listed in the playlist.)
If you’re using “SHOUTcast,” there will be download buttons in the radio program list.
As seen in Figure 6, there is “Winamp,” “M3U,” and “X3PF” listed in the dropdown list. Select “M3U” and begin the download. This will download a file named “tunein-station.m3u” which you can open up in a text editor to see the following. (*Image reflects file at the time of writing (February 13th, 2015))
#EXTINF:-1,(#1 - 212/5000) SmoothJazz.com Global Radio - The World's First 24/7 Since 2000 - KJAZ.DB
#EXTINF:-1,(#2 - 563/5000) SmoothJazz.com Global Radio - The World's First 24/7 Since 2000 - KJAZ.DB
#EXTINF:-1,(#3 - 5/6) SmoothJazz.com Global Radio - The World's First 24/7 Since 2000 - KJAZ.DB
Drop this playlist file into the ” /var/lib/mpd/playlists ” directory on your Raspberry Pi. That’s all there is to it! Take a look with the “ls” command, and…
“tunein-station” is displayed right next to the “test1” we just created!
Next, use the “load” command to load the playlist.
mpc load tunein-station
If you see “loading: tunein-station” displayed in the console, then it was loaded just fine! Take a look at the uploaded playlist, and…
The content of “tunein-station.m3u” shows up! “test1_list” was still loaded, so “tunein-station.m3u” was added onto it. Executing the “playlist” command will show the “current playlist,” as seen in the Help. This is a little confusing, since the file saved is also called a playlist. (I had a really hard time with this until I figured it out.)
When you want to empty the playlist and start over, use the “clear” command.
To use Internet radio with my iPhone5, I downloaded an app called MPoD. There doesn’t seem to be many iOS apps that support what we’re doing. Android has a lot, though, so search for “MPD” and pick out a good looking one for your phone. https://play.google.com/store/search?q=mpd&c=apps
All you need to do is installing the app on your smartphone.
If the app detects playlists, it will automatically load them and display them on the screen. The screenshot here shows three MP3 files up top, followed by the SHOUTcast playlist file content. (I used the M3U file that we downloaded earlier, so there are a few duplicates in the list.)
Touch the radio program name and it will begin to play through your Raspberry Pi speakers.
Like most other audio player apps, this one has play, pause, and volume control functions. It almost feels like a remote control for your Raspberry Pi!
You can also switch playlists via the app. Press the “Menu” on the top left of the screen, which will make a menu show up on the left.
Choose “Saved playlist” from here to display the saved playlists in your Raspberry Pi playlist folder (/var/lib/mpd/playlists/).
Press the name of a playlist to load it up. This works just like the “load” command.
Return to the “playlist” screen from the menu, and you’ll see the playlist you just added. It will have been added onto the existing playlist, so you’ll see duplicates. This will continue to happen each time you load a new playlist. Press “Edit” on the top right of the “playlist” screen and then “Clear playlist.”
Playlists can be edited through a text editor. By adding your favorite stations to make an original playlist, you’ll get a lot more fun out of your radio! There are a lot of stations out there aside from what’s available on SHOUTcast, so get looking and give one a try!
#SHOUTcast-SmoothJazz.com Global Radio
Load your new playlist into MPoD and this is what you’ll see!
Some stations will display the radio program and/or song names once you tune in. I thought being able to set the program name myself might be nice, so I tried out a couple of things.
http://220.127.116.11:80/#SHOUTcast-SmoothJazz.com Global Radio
Simply end the URL with a “/” then type “#” followed by the title, and that title will show up if you’re using SHOUTcast.
With this, the overhaul of our Raspberry radio is complete!
Using an app to control it not only made it simpler, but also increased its usability. Being able to control the radio like a music player allows for easy operation, and helps you to convince others to give it a try because all it takes is for them to download an app! This radio is definitely going to see its fair share of use around my office. (If it were up to me, every household would have one!)
Here what’s coming next.
What is this familiar box waiting for me on my work desk? It’s the latest “Raspberry Pi2 Model B”! (I couldn’t help but snap a photo to commemorate its arrival, haha.)
On the subject of “Raspberry Pi2,” people said that Windows10 is going to be available for free! Since it’s a “2” and all, how much better is it compare to the previous model? I can barely contain my excitement. I will be playing with the Raspberry Pi2 next time!