continuous music playlist with the raspberry pi
2013-Mar-16, Saturday 11:48 pm
A quick comment for some good news, though. Do you remember when I told you that audio is borked on the pi? Well, the Raspberry Pi Executive Director agrees. He posted on January 1st a job position specifically for work on the ALSA audio driver. I look forward to some fixes in future Raspbian builds. Now back to the fun stuff.
It takes a small amount of setup mojo to get it working, but I like the mpd server and client software for the usage that I envision. In my case, we need a continuous audio feed for our phone system. It will play the music-on-hold that you hear while waiting for your phone call to route. We could also use a similar system to play the audio enrichment music for dogs, cats, and other critters in our shelters. We want the audio files to play continuously as long as the pi is powered up. Here are the commands that you will need to get your pi working.
sudo apt-get install mpd mpc
mkdir -p /home/pi/.mpd/playlists
mkdir /home/pi/Desktop/music
touch /home/pi/.mpd/{mpd.db,mpd.log,mpd.pid,mpdstate}
ls -al /home/pi/.mpd
sudo vi /etc/mpd.conf
You are now editing the default global config file for mpd. (Don't use a local version. It causes lots of file permission pains. I wasted hours before finding this global file.) It needs to contain the server configuration variables. Here is the info that worked for me. You should be able to use the same text. Either modify the lines that are already there, or delete the contents and insert just these lines.
music_directory "/home/pi/Desktop/music"
playlist_directory "/home/pi/.mpd/playlists"
db_file "/home/pi/.mpd/mpd.db"
log_file "/home/pi/.mpd/mpd.log"
pid_file "/home/pi/.mpd/mpd.pid"
state_file "/home/pi/.mpd/mpdstate"
audio_output {
type "alsa"
name "mpdaudio"
}
bind_to_address "127.0.0.1"
That last line is necessary as a hard-coded ip4 address. Most tutorials suggest that you use "localhost" instead. I did, but I kept getting errors. Apparently the Raspberry Pi network setup has both ip4 and ip6 assignments for localhost, but mpd gets confused by the duplication. Hardcode the ip4 address as listed above, and you're good. You have mpd working as a system service at startup.
Copy your .mp3 files into your Desktop/music folder. The server will see whatever files are in it. Keep the filenames in alphabetical order, so it simplifies your playlist ordering. Now we need to use the client to make sure the server is playing at the proper volume and in a repeat loop. We want it to check for updates daily.
cd /etc/cron.daily
sudo touch mpdrefresh.sh
sudo chmod 755 mpdrefresh.sh
sudo vi mpdrefresh.sh
Insert these commands into the file.
# Refresh the mpd music database
mpc volume 100
mpc repeat on
mpc update
mpc clear
mpc add /
mpc play
If you want, you can see when these daily scripts will execute. Use "more /etc/crontab" to see the listing. Notice that scripts in the cron.daily folder are executed every day at 06:25. We also want to run the update at boot time, just for our own peace of mind. We'll need to update the startup script.
sudo vi /etc/rc.local
The last line of the startup script should be "exit 0". Before that line, insert this command.
# Refresh the mpd music database
/etc/cron.daily/mpdrefresh.sh
Save and exit. If you want to test immediately, you can use these commands to place a soothing river sound for playback.
cd ~/Desktop/music
wget http://www.soundjay.com/nature/river-4.mp3
sudo reboot
If all goes well, you are listening to your music files play in a continuous loop soon after your pi powers up. If you need to do any troubleshooting, these commands should help you stop the server daemon.
ps -ef | grep mpd
sudo service mpd stop
sudo pkill mpd
If you need additional help, then check out this extensive mpd guide.