obsolete.computer/geekery/

Streaming Music to an Android Phone

I finally got around to setting up a streaming audio server at home so I could listen to my music collection on my phone from anywhere. Here's the basic gist of how to do it.

1. Get an MPD server going.

If you don't use MPD yet, you're missing out. MPD is the Music Player Daemon, which basically means it's a music player designed to run as an invisible system service and be managed by one of literally hundreds of different clients, depending on your use case.

How you install MPD will depend on what OS you're using. On Arch Linux, MPD is in the extra repository, so a simple `sudo pacman -S mpd` will install it on your system (there are setup instructions in the Arch Wiki). On Ubuntu, it should be in the Ubuntu Software Center. On Windows[1], well I have no idea but it shouldn't be to tough (try browsing the MPD Wiki for Windows-specific instructions). You'll also want to install either Lame or Vorbis, depending on whether you want an Ogg or MP3 encoded stream.

Since MPD will be accessible from the internet, make sure you exercise your due security diligence by setting it up to run as a user without a login shell. I'd also recommend using non-standard ports for MPD and streaming. The defaults are 6600 for MPD control and 8000 for streaming. Pick two of your own numbers (greater than 1024 and less than 65535, though I recommend going for the higher end of the spectrum) and write them down. Also come up with a secure password to assign MPD. Here's what a basic /etc/mpd.conf looks like to enable some security and allow http streaming:

music_directory         "/home/YOUR_USERNAME/Music"
playlist_directory      "/var/lib/mpd/playlists"
db_file                 "/var/lib/mpd/mpd.db"
log_file                "/var/log/mpd/mpd.log"
pid_file                "/var/run/mpd/mpd.pid"
state_file              "/var/lib/mpd/mpdstate"
user                    "mpd"
port                    "INSERT_YOUR_DESIRED_MPD_PORT_HERE"
password                "MAKE_UP_AN_MPD_PASSWORD_HERE@read,add,control,admin"
default_permissions     "read" #Leave empty to keep people from viewing your MPD instance without the password
bind_to_address         "0.0.0.0"

audio_output {
    type "alsa"
    name "Onboard Audio"
}

audio_output {
    type "httpd"
    name "HTTP Stream"
    encoder "lame" # optional, vorbis or lame
    port "INSERT_YOUR_DESIRED_STREAMING_PORT_HERE"
    #quality "5.0" # do not define if bitrate is defined
    bitrate "96" # do not define if quality is defined
    format "44100:16:2"
    bind_to_address "0.0.0.0"
}

2. Set up Dynamic DNS on your router.

Most routers have Dynamic DNS support these days. Dynamic DNS is a service that lets you point a domain name at your home router. This way, when your router's publicly-visible IP address changes, you can still access it without having to know what the address changed to.

As a preliminary step, you'll want to give your streaming machine a static IP address or a DHCP reservation (whichever applies to your network). Once this is done, you can also control MPD on your home network via Wifi.

Setting up DDNS on your router will vary depending on the manufacturer. But in generic terms, here's what you'll want to do:

  1. Sign up for a free Dynamic DNS service, such as dyndns.org. Don't worry, they have a free plan, you just might have to poke around their website a bit to find it. Pick a meaningful domain name that's not too difficult to remember. Unless you sign up for a premium service, your domain name will be something like yourdomainname.dyndns.org.
  2. Log into your router and find the DDNS option. On my Netgear, it was one of the main options on the left.
  3. Put your Dynamic DNS account credentials into the DDNS setting on your router. If DDNS registration is successful, you should be able to check the logs on the router and see that it signed into your DDNS account, and you can also check by issuing the command `nslookup yourdomainname.dyndns.org` from your computer which should return your router's external IP.

3. Open up the ports you chose on your firewall

On your router, open up the two ports you chose for MPD control and streaming, and forward them to the IP address of your MPD machine. Look for an option called port forwarding in your router's configuration.

4. Install MPDroid on your phone.

MPDroid is one of those hundreds of MPD clients I mentioned earlier. It's free in the Android Market. Install it and go into the default MPD Server settings, where you'll plug in your hostname (the DDNS name), port, password, and streaming port. Once those settings are saved, go back to the main screen and if it's connected to your MPD server hit play. Then hit the menu button and select Streaming. Playback should start in a few moments.

And voila, that's it! Of course you'd also benefit from installing a local client on your MPD machine, so that you can locally play and manage your music.

Troubleshooting

If it doesn't work, here are a few hints I picked up along the way:

  • Make sure it works on your internal network first. This is actually how I used it for a long time before I opened up ports to the outside. If you do this, just create a Wifi-network-specific configuration in MPDroid and point it at your MPD machine's internal IP address rather than your external DDNS name. Everything else should work the same.
  • Make sure you restart MPD when you edit the config file. This can be done by issuing (on Debian/Ubuntu/Fedora/etc): `sudo /etc/init.d/mpd restart` or (on Arch):`sudo /etc/rc.d/mpd restart`
  • On your phone, make sure Wifi is enabled. This was a big gotcha for me because I was testing it by turning the Wifi off. But apparently MPDroid doesn't properly access your MPD server over 3G unless the Wifi chipset is turned on, even if you're not actually connected to a Wifi network.
  • Also make sure the ports you chose for MPD aren't being used by any other services. To see what ports are currently in use, you can use the netstat command (on both Windows and Linux).

Enjoy!

Now go forth and enjoy the fruits of do-it-yourselfism[2] abroad!

[1]: Edit: It appears the Windows port is still in development, sorry to get your hopes up if you're a Windows user. I guess there's no better time than the present to set up a Linux box!
[2]: Do-it-yourselfism is of course a myth, none of this would be possible without the work and generosity of thousands of free-software developers! Oh how I do enjoy software liberty.


Modified Wednesday, March 24, 2021