Hack 83. Command-Line Streaming MP3 Player
Use basic command-line tools to create your own streaming MP3 player.
When setting up Obsequeium (http://obsbox.sf.net) or Jinzora (http://jinzora.org) web jukeboxes [Hack #84], I've found it helpful to set up a dedicated streaming player so I can listen to my net jukebox constantly while testing. Preferably the MP3 stream should never stop and, should something happen to the playback, it should pause for a moment and try the stream again. It may seem the mpg123/mpg321 command-line players are perfect for this task, but they have a habit of locking up and not exiting or retrying when something goes wrong with the stream. This makes them unsuitable for use as dedicated streaming players.
You can build a robust command-line streaming player with one command if you have madplay and wget installed. wget and madplay are both popular programs and should be prepackaged by your Linux distribution. Use your distribution's software installation tool to install these programs. If for some reason you don't have these tools prepackaged, download the tarballs from http://www.underbit.com/products/mad/ and http://www.gnu.org/software/wget/wget.html, compile, and install them according to the included installation instructions.
With wget and madplay installed, the following command will play the stream from http://example.com/mystream:
$ wgetqO http://example.com/mystream | madplay -Q --no-tty-control-
wget reads the MP3 stream and quietly writes it to stdout which gets piped to madplay' sstdin. From there, madplay decodes the stream and writes it to the default sound device. TheQ andno-tty-control options tell madplay to play the music without any text output and places the process in the background so it won't take over the terminal.
There is one more gotcha to overcome before our robust streaming MP3 player can run for days on end. MP3 streams that run for days have this nasty habit of dying right as you're grooving to your favorite track. To prevent this from happening, call the streaming player in a simple shell script loop:
#!/bin/sh while [ 1 ] do wget -q -O -http://example.com/mystream | madplay -Q --no-tty-control- sleep 5 done
Should something happen with the stream and the player exits, it will pause for five seconds and then restart the stream again. The five-second pause prevents the player from pummeling your streaming server with connection requests in case something happens with the server.
Finally, to ensure the robustness of the command-line player, I recommend killing the player once a day at a slack time to restart the stream and make sure that it doesn't die in the middle of your favorite track. Use cron to accomplish this by adding this line to your crontab (edit your crontab with crontabe):
0 4 * * * killall9 madplay
This causes cron to kill madplay each day at 4 A.M. Pick a time when you expect the least number of people to listen, because listeners will need to sit through five seconds of silence.
That completes our hassle-free robust streaming MP3 player. This player configuration has played music for me for nearly two years without requiring human attention.