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.
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 Offical Site
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!
1 2 |
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)
1 |
# 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)
1 |
auto_update "yes" |
/etc/mpd.conf (around line 322)
1 |
mixer_type "software" |
When you’re done with the configuration, restart MPD from the LX Terminal.
1 |
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.
1 |
mpc help |
Figure 1
Figure 2
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!
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.
Figure 3
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.
1 |
mpc update |
Let’s check the list of files to make sure that everything was loaded. Execute the “ls” command.
1 2 |
mpc ls mpc ls test1 |
Figure 4
“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.
1 |
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!
1 |
mpc play |
Figure 5
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.
1 |
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.
/var/lib/mpd/playlists/test1_list.m3u
1 2 3 |
test1/Beethoven-SymNo7-1-Piano.mp3 test1/Holst-Planets-Jupiter.mp3 test1/Rhapsodyinblue-2Piano.mp3 |
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.
1 |
mpc load test1_list |
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.
Figure 6
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))
tunein-station.m3u
1 2 3 4 5 6 7 |
#EXTM3U #EXTINF:-1,(#1 - 212/5000) SmoothJazz.com Global Radio - The World's First 24/7 Since 2000 - KJAZ.DB http://173.244.199.249:80 #EXTINF:-1,(#2 - 563/5000) SmoothJazz.com Global Radio - The World's First 24/7 Since 2000 - KJAZ.DB http://50.115.122.54:80 #EXTINF:-1,(#3 - 5/6) SmoothJazz.com Global Radio - The World's First 24/7 Since 2000 - KJAZ.DB http://75.126.221.202:8000 |
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…
1 |
mpc ls |
Figure 7
“tunein-station” is displayed right next to the “test1” we just created!
Next, use the “load” command to load the playlist.
1 |
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…
1 |
mpc playlist |
Figure 8
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.
Figure 9
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.
Figure 10
Choose “Saved playlist” from here to display the saved playlists in your Raspberry Pi playlist folder (/var/lib/mpd/playlists/).
Figure 11
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.”
Figure 12
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!
/var/lib/mpd/playlists/myradio.m3u
1 2 3 4 |
#SHOUTcast-SmoothJazz.com Global Radio http://173.244.199.249:80 #SHOUTcast-Jazzradio http://216.230.231.164:9938 |
Figure 13
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.
1 |
http://173.244.199.249: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.
Picture 1
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!