Hass remote speakers with MPD - Android

August 20, 2023

This is part of a series of posts on using MPD to set up remote speakers controlled by HomeAssistant on various devices. This post covers Android; specifically, three old android devices (bullhead, sargo, and flox) running LineageOS.

Android has an official mpd package which is set up for network control out of the box, so we should be good to go, right? Sadly, it suffers from a bug where the last second or two of audio gets cut off. This is the mirror image of the slow-to-wake amp described earlier, cutting off the last few words of long announcements and dropping short sounds entirely.

Fortunately, mpd is also available in termux, and while that version suffers from the same bug, it gives us more options to work around it. I’m not going to rehash how to set up termux or configure ssh access, since I wrote on that topic earlier, but once you have termux installed it’s actually easy to set up; the hard part is figuring out what sort of setup you should be aiming for.

You’ll need both mpd itself and the sox package to provide audio playback. I also like to install termux-services to manage the daemon, but you can use whatever you like (that, Termux:Boot scripts, manual login and screen, etc) for service management; I won’t go into that here.

$ pkg update
$ pkg install mpd sox
$ nano $PREFIX/etc/mpd.conf

Most of the settings in mpd.conf can be left as is, but we will need to make sure that network control is enabled, along with the curl input so that we can push media to it remotely. Note that it has to listen on 8600 rather than the default (6600) due to limitations on which ports termux is allowed to open.

bind_to_address "any"
port "8600"
default_permissions "read,add,control"
restore_paused "yes"
input {
  plugin "curl"
}

It also comes with a default sles audio output, but we are going to replace it with an external program, sox’s play, which does not suffer from the early cutoff bug that sles has. Don’t forget to delete or disable the sles output as well!

audio_output {
  type "pipe"
  name "sox-pipe"
  format "48000:16:1"
  command "play -q -t raw -e signed-integer -r 48k -b 16 -c 1 -"
}

This configuration will have mpd decode the audio, but pipe the PCM frames to play rather than trying to play them directly; play will then manage the actual audio output.

And…that’s it! Start up mpd and you should be good to go. Try connecting to it (using mpc or netcat) from another machine and make sure you can add songs to it using http:// and https:// URIs.