In order to learn capoeira songs, I planned to listen to them when I take my shower. And I would the system to be as simple as possible : just one click.
First of all, buy a bluetooth speaker. I bought this one : http://fr.jbl.com/enceintes-portables/JBL+GO.html
Cheap, nice, light, quite long autonomy. Do the job.
Next step : make it connect automatically to a computer, so that sound will be player trough it.
Of course I took a Linux. As I have a Raspberry Pi always on, why not use it ?
Here come the trickiest part : make the BT speaker connect or be connected as soon as it’s on. Solution is here : https://www.raspberrypi.org/forums/viewtopic.php?f=63&t=107144
To summerize :
$ vi /etc/bluetooth/audio.conf
# Add this line :
$ sudo reboot
$ hcitool scan
$ sudo bluez-simple-agent hci0 AA:BB:CC:DD:EE:FF
$ sudo bluez-test-device trusted AA:BB:CC:DD:EE:FF yes
Now, as soon as the speaker is powered on, it will be recognized and connected.
Next step: make sound through it.
I use Alsa, as I don’t want to install PulseAudio, which requires more configuration:
$ vi /etc/asound.conf
Now, my default sound output is my BT Speakers.
Next: let’s play!
I use MPD. MPD automatically plays when launched, continuing where it stops. I can give him parameters when it’s on (random, playlist, …) and they will be kept.
I just have to tell him to play through Alsa.
$ vi /etc/mpd.conf
One more thing…
Ok, now power on the speaker, feed mpd with the music you want, play it. Ok, I got sound.
BUT if I switch the speaker off, mpd become crazy and switch the speaker back on doesn’t make it recover its sanity: it doesn’t play anymore.
Maybe should I use an other player, maybe should I look to dbus to see if mpd (or whatever) can react to dbus events, maybe should I use Pulseaudio, but I choosed to make a little shell script.
echo "$(date): "
while [ "$1" != "" ]
case $1 in
[ -e $PIDFILE ] && kill $(cat $PIDFILE) && rm -f $PIDFILE && exit
if [ -e "$PIDFILE" ]
echo "Already running: $(cat $PIDFILE)"
echo $$ > $PIDFILE
trap "rm -f $PIDFILE" 0
pgrep /usr/bin/mpd ; _mpd=$?
#sdptool browse $JBL ; _ec=$?
l2ping -t 1 -c 1 $JBL 2>/dev/null | grep -q '1 received' ; _jbl=$?
if [ $_jbl -eq 0 ] # If bt headset is reachable
if [ $_mpd -eq 1 ] # but mpd is not running
echo "$(now) BT is there. Let's play !"
/etc/init.d/mpd start >/dev/null
mpc play >/dev/null
mpc update >/dev/null
mpc random on >/dev/null
mpc repeat on >/dev/null
mpc | grep playing || (mpc load capoeira.m3u && mpc play)
else # headset is not reachable
if [ $_mpd -eq 0 ] # mpd is running
echo "$(now) BT is gone"
/etc/init.d/mpd stop >/dev/null
Run it as root whith
sudo nohup radio-bathroom.sh >/dev/null &
And you are on : power on the speaker, and music will come after few seconds. Power it off, mpd will stop.