Raspberry Pi streaming internet radio to bluetooth

Introduce yourself, create test postings or talk nonsense
Post Reply
User avatar
mimosa
Salix Warrior
Posts: 3000
Joined: 25. May 2010, 17:02
Contact:

Raspberry Pi streaming internet radio to bluetooth

Post by mimosa » 27. May 2018, 09:33

I may finally have found a use for my trusty Pi Model B. It was quite tricky to get it to play with my bluetooth speaker, but I was eventually able to do so using this very clear guide:
https://www.sigmdel.ca/michel/ha/rpi/bl ... 02_en.html
This method is with ALSA and bluez-alsa:
https://github.com/Arkq/bluez-alsa
My speaker now automatically pairs and connects when turned on, so that part is good. To get it to actually play the audio stream, the guide recommends creating the following .asoundrc:

Code: Select all

defaults.bluealsa.interface "hci0" 
defaults.bluealsa.device "30:21:3E:31:C6:2B" 
defaults.bluealsa.profile "a2dp" 
defaults.bluealsa.delay 10000
Then once you have turned the volume up in alsamixer, the stream can be played by something like:

Code: Select all

mpv --audio-device: alsa/bluealsa "mms://mediau.yle.fi/liveradiopuhe" # Finnish radio
which is from memory, but I installed mpv, mplayer and vlc to try them all, and each has a different syntax; all work.

The problem is, I want to run it headless, constantly streaming audio (if I want to listen to a different station, I can ssh into the Pi and change it). Then I turn my speaker on in the morning, without having to switch on the Salix desktop and get sucked into the internet and its distractions, and listen to the news in Finnish.

This might be rather like the sort of "plug and play" behaviour you see when using normal AV applications on the desktop - you plug in your headphones, and the loudspeakers go off, if they were turned on in the first place.

But what happens is, whichever player I'm using tries to buffer the stream, and complains tremendously, as soon as the speaker is turned off. Quite apart from that, the Pi expects the speaker to be connected before the stream is started. But then, I have to ssh into it and do it manually, which defeats the whole object.

Does anyone have any suggestions? I am reluctant to mess with the bluetooth part, which works - I tried every which way with pulse, and it was never successful.

User avatar
gapan
Salix Wizard
Posts: 5356
Joined: 6. Jun 2009, 17:40

Re: Raspberry Pi streaming internet radio to bluetooth

Post by gapan » 27. May 2018, 17:09

Not a direct solution to your problem, but have you thought about using a distribution that is dedicated to audio playback like volumio or pimusicbox?
https://volumio.org/
http://www.pimusicbox.com/

You could easily turn it on/off or manage it in any way from your smartphone or hardware buttons you put on the pi itself.

Hardware buttons/switches connected to the pi's GPIO ports could also be used to start/stop applications with your current setup I guess too, requiring some minimal scripting and some wiring of course.
Image
Image

User avatar
mimosa
Salix Warrior
Posts: 3000
Joined: 25. May 2010, 17:02
Contact:

Re: Raspberry Pi streaming internet radio to bluetooth

Post by mimosa » 28. May 2018, 08:32

Thanks, I wasn't aware of those. In my case it would have to be the buttons (which are in any case more in the spirit of the Pi).

You could do it with one button that would restart the stream, after you connected the speaker.

The approach I was thinking of was to create a dummy sound "device" that Alsa would send the stream to when the speaker was not connected, but make the speaker the default so the stream would be diverted to it when available.

Alternatively, perhaps a script that would detect the speaker going on and off and start and stop the stream accordingly. That might actually be easier than delving into the fine points of Alsa.

I must be the last person on the planet who doesn't have a smartphone. But if I did have one, it would defeat the present object, which is to listen to an internet stream without otherwise connecting to the internet and looking for that "important" email or news. The Guardian website gets me every time :lol:

User avatar
mimosa
Salix Warrior
Posts: 3000
Joined: 25. May 2010, 17:02
Contact:

Re: Raspberry Pi streaming internet radio to bluetooth

Post by mimosa » 29. May 2018, 14:56

As an interim solution, I am just leaving the speaker on all the time with the volume muted.

After connecting to it initially through bluetooth, it is enough to do

Code: Select all

ssh pi@raspberrypi
player <stream> &
disown && exit
I wasn't familiar with the disown command, which I think is rather nice. It means the process isn't killed when you exit the shell.

User avatar
mimosa
Salix Warrior
Posts: 3000
Joined: 25. May 2010, 17:02
Contact:

Re: Raspberry Pi streaming internet radio to bluetooth

Post by mimosa » 10. Jun 2018, 13:12

I've now got round to writing a little script to turn the stream on and off according to whether the speaker is connected, which means I control it with its on and off switch instead of just turning the volume down:

Code: Select all

#!/bin/bash
SWITCH='OFF'

PLAYER='mpv '
OPTIONS='--audio-device=alsa/bluealsa '
STREAM='mms://mediau.yle.fi/liveradiopuhe ' 

while (sleep 10)
do
        if [ `busctl tree org.bluez | wc -l` -lt 5 ] ; then # this tells us the speaker isn't connected, TODO find a better way
#               echo "The speaker is off"
                if [ $SWITCH = 'ON' ] ; then
                        killall $PLAYER
                        SWITCH='OFF'
                fi
        else
                if [ $SWITCH = 'OFF' ] ; then
#                       echo "The speaker has just been turned on"
                        killall $PLAYER
                        $PLAYER $OPTIONS $STREAM > /dev/null 2>&1 &
                        SWITCH='ON'
                fi
        fi

done
The way I'm testing for that with busctl tree is hackish, but works. It's less expensive that querying the bluetooth stack directly.

User avatar
gapan
Salix Wizard
Posts: 5356
Joined: 6. Jun 2009, 17:40

Re: Raspberry Pi streaming internet radio to bluetooth

Post by gapan » 10. Jun 2018, 19:20

Nice!
Image
Image

User avatar
laprjns
Salix Warrior
Posts: 1027
Joined: 28. Aug 2009, 01:30
Location: Connecticut USA

Re: Raspberry Pi streaming internet radio to bluetooth

Post by laprjns » 10. Jun 2018, 21:46

mimosa wrote:
10. Jun 2018, 13:12
The way I'm testing for that with busctl tree...
Given yourself to the dark side have we.

User avatar
mimosa
Salix Warrior
Posts: 3000
Joined: 25. May 2010, 17:02
Contact:

Re: Raspberry Pi streaming internet radio to bluetooth

Post by mimosa » 11. Jun 2018, 06:46

The Pi runs Debian, so, yes :twisted:

There is actually a version of Slack for it, which I was going to try out, but since it works ... I have it on all the time with irssi running in a detached screen session, so I'm back in the Salix chat, too.

User avatar
gapan
Salix Wizard
Posts: 5356
Joined: 6. Jun 2009, 17:40

Re: Raspberry Pi streaming internet radio to bluetooth

Post by gapan » 11. Jun 2018, 06:48

Slackware 14.2 is softfloat, so I'm not that willing to try it. In current they switched to hardfloat, so next slackware release will be a better match for the pi!
Image
Image

Post Reply