TTS on Bluetooth speaker via Raspberry Pi

@jruben4 and @oakbrad did you ever figure it out? I have the same problem:

pi@hassbian:~ $ mplayer -ao pulse::bluez_sink.68_37_E9_91_15_DD -volume 50  /tmp/crowd.mp3
MPlayer2 2.0-728-g2c378c7-4+b1 (C) 2000-2012 MPlayer Team
Cannot open file '/home/pi/.mplayer/input.conf': No such file or directory
Failed to open /home/pi/.mplayer/input.conf.
Cannot open file '/etc/mplayer/input.conf': No such file or directory
Failed to open /etc/mplayer/input.conf.

Playing /tmp/crowd.mp3.
Detected file format: MP2/3 (MPEG audio layer 2/3) (libavformat)
[mp3 @ 0x75be15b8]max_analyze_duration 5000000 reached
[mp3 @ 0x75be15b8]Estimating duration from bitrate, this may be inaccurate
[lavf] stream 0: audio (mp3), -aid 0
Load subtitles in /tmp/
Selected audio codec: MPEG 1.0/2.0/2.5 layers I, II, III [mpg123]
AUDIO: 44100 Hz, 2 ch, s16le, 128.0 kbit/9.07% (ratio: 16000->176400)
AO: [pulse] Init failed: No such entity
Failed to initialize audio driver 'pulse::bluez_sink.68_37_E9_91_15_DD'
Could not open/initialize audio device -> no sound.
Audio: no sound
Video: no video

Having problems with a Bose Mini Soundlink II

homeassistant@dadraspi3:/home/pi $ bluetoothctl
[NEW] Controller B8:27:EB:DC:02:47 dadraspi3 [default]
[NEW] Device 04:52:C7:56:91:FF Bose Mini II SoundLink

but not showing up

homeassistant@dadraspi3:/home/pi $ pactl list short | grep bluez
12 module-bluez5-discover
13 module-bluez4-discover

tts not showing in homeassistant menu

further investigations - this is not a homeassistant problem - its a Pi + Bose + Bluetooth problem, no connection.
tried the following, now connected:

āÆ bluetoothctl
[bluetooth]# agent on
[bluetooth]# scan on
[NEW] Device xx:xx:xx:xx:xx:xx Bose Mini II SoundLink
[bluetooth]# trust xx:xx:xx:xx:xx:xx
[bluetooth]# connect xx:xx:xx:xx:xx:xx
Attempting to connect to xx:xx:xx:xx:xx:xx
[CHG] Device xx:xx:xx:xx:xx:xx Connected: yes
Connection successful

the speaker has to be in pair mode! - does not survive a re-boot
and tts still not showing in services menu of homeassistant

@eiolos Nope I never could get the notification to work through a bluetooth speaker. I could get it to play audio via mplayer but not through the TTS notification.

@pkozul thanks a lot for the post! really helpful. just wondering if there is a way to install pulseaudio for Hass.io? Iā€™m really new to HA and raspberry pi and cant really tell the difference between hassio and hassbian. I installed hassio for my machine since its easier to set up. thanks much!

1 Like

I could never get it to work either.

could we have a hassio addon for this ?

3 Likes

Iā€™d like to second this. A HASSIO add-on would be fantastic.

3 Likes

Hi,
Iā€™m trying to run tts_bluetooth_speaker.
2017-09-25 03:53:23 ERROR (MainThread) [homeassistant.loader] Unable to find component media_player.tts_ bluetooth_speaker
return this message.
my tts_bluetooth_speaker.py is at /home/homeassistant/.homeassistant/custom_components/media_player
Any suggestion?
should it go somewhere else?

After much finagling, I can get mplayer to manually play the mp3 generated in /tmp/tts but itā€™s still not happening automatically.

The Logbook shows the bluetooth tracker going off and then on, but thereā€™s now mention of trying to play the file in between or otherwise.

Systemctl status check on HA shows:

AO: [pulse] Init failed: Access denied
Failed to initialize audio driver 'pulse::bluez_sink.FC_58_FA_D0_1C_CA'
Could not open/initialize audio device -> no sound.
Audio: no sound
Video: no video
Exiting... (End of file)

pactl stat shows:

Default Sink: bluez_sink.FC_58_FA_D0_1C_CA

When I try to run pacmd commands I get:

No PulseAudio daemon running, or not running as session daemon.

syslog has:

hassbian pulseaudio[423]: W: [pulseaudio] protocol-native.c: Denied access to client with invalid authorization data.

Added homeassistant to audio group (was using pi). Voila!

sudo gpasswd -a homeassistant audio

I faced this issue before. Connect to the device before you run mplayer -ao pulse::bluez_sink.88_C6_26_AC_41_1F -volume 50 /tmp/crowd.mp3 command

it works for me.

You did a absolute great work. I had no problem with your solution on raspbian jessi. Only the bcm chip bug ( Bluetooth audio stutters when using internal WiFi and Bluetooth) on the pi3 sucks a bit. I found a info in the relnotes of stretch that the problem is fixed, so I did upgrade to stretch. Now the pairing wonā€™t work automatically and hass donā€™t push the tts to the Bluetooth speaker. It looks like there is a conflict with pulseaudio and the new included bluezalsa. Can you check what I have to change in your code to use bluezalsa.

Ok a little confusion. Currently have a raspberry pi running mpd via the connected through the 3,5 jack speakers.

Want to add a bluetooth TTS. But iā€™m confused. Pulse will become the default audio device so mpd will play through the 3,5 jack and bluetooth at the same time? Ot it is possible to let MPD continue playing through the jack and the announcements to go through the bluetooth?

I wound up running into the issue where mplayer spawned by hass would fail with the following, but the manually run command would work:

MPlayer 1.3.0-7 (C) 2000-2016 MPlayer Team
do_connect: could not connect to socket
connect: No such file or directory
Failed to open LIRC support. You will not be able to use your remote control.

Playing /tmp/tts_c913a0cada7c35997625389957af03316efe6334_en_-_google.mp3.
libavformat version 57.71.100 (external)
Audio only file format detected.
Load subtitles in /tmp/
==========================================================================
Opening audio decoder: [mpg123] MPEG 1.0/2.0/2.5 layers I, II, III
AUDIO: 24000 Hz, 2 ch, s16le, 32.0 kbit/4.17% (ratio: 4000->96000)
Selected audio codec: [mpg123] afm: mpg123 (MPEG 1.0/2.0/2.5 layers I, II, III)
==========================================================================
AO: [pulse] Init failed: No such entity
Failed to initialize audio driver 'pulse::bluez_sink.< bluetooth address >'
Could not open/initialize audio device -> no sound.
Audio: no sound
Video: no video


Exiting... (End of file)

ā€œpactl list sinksā€ showed the device name as ā€œbluez_sink.< bluetooth address >.a2dp_sinkā€, so I worked around this by simply adding ā€œ.a2dp_sinkā€ to the address in the configuration file.

  - platform: tts_bluetooth_speaker
    address: < bluetooth address >.a2dp_sink
    volume: 0.45
    pre_silence_duration: 1       # Optional - No. of seconds silence before the TTS (default is 0)
    post_silence_duration: 0.5    # Optional - No. of seconds silence after the TTS (default is 0)
    cache_dir: /home/homeassistant/.homeassistant/tts           # Optional - make sure it matches the same setting in TTS config
1 Like

I realize this is an older post, but I ran into the same problem with my paired Echo where it suddenly stopped playing after working for a couple of weeks. I havenā€™t figured out how to fix it but thought Iā€™d post in case someone else runs into the same issue.

Essentially what has happened is the speaker is now being identified as a source (input) and not a sink (output). Therefore mplayer canā€™t play to it.

In my case, whenever I previously connected to my Echo, only the bluez_card would show (i.e., no sources or sinks). I could enable a sink by ā€œpactl set-card-profile [index] a2dpā€. Now, when I try to change the profile to a2dp I get a ā€œFailure: Input/Output errorā€ and a pulseaudio log of ā€œmodule-bluez5-device.c: Refused to switch profile to a2dp: Not connectedā€

For the failing Echo case, it appears after connecting it is defaulting to a2dp_source which does not create a sink and effectively pipes the Echo speakers to the RPi (instead of the other way). Thus the Echo wonā€™t respond to voice command as itā€™s piping its output to the RPiā€™s input. Here is a link where I found it reported without a solution beyond buying a bluetooth dongle.

For those that it helps, I downgraded my pulseaudio to the 7.1 in jesse-backports and can now access my Amazon Echo again as a sink. Apparently when I updated my RPi to stretch it moved me up to 10.0.

EDIT: So I did a little more testing and itā€™s not related to the version. Iā€™m now back to pulseaudio in stretch and got it working. Apparently, it makes a difference on how you connect to the Echo. I found that if you ever use the Alexa app to connect to the RaspberryPi using a dongle or the native bluetooth, it will result in the a2dp_source behavior. However, if you only connect from the RPI using bluetoothctl only, then it results in the a2dp_sink being available.

1 Like

Thank you for this awesome contribution.

Can more bluetooth speakers be added to the configuration?

eg. Bluetooth speaker in bedroom, bluetooth sound bar in TV room and bluetooth speaker in lounge

media_player bt_audio:
  - platform: tts_bluetooth_speaker
    name: JBL pulse
    address: 00:2F:AD:12:0D:42
    volume: 0.45
- platform: tts_bluetooth_speaker
    name: JBL charge
    address: 00:2F:AD:12:0D:43
    volume: 0.45   
- platform: tts_bluetooth_speaker
    name: LG Sound bar
    address: 00:2F:AD:12:0D:44
    volume: 0.45

Anyone tried with one of these?

LIGHTSTORY Bluetooth Light Bulb Speaker, E27 Base 8W 6500K Color Changing Smart Music Bulb, RGB LED Bulb, Wireless Bluetooth Speaker Bulb

Would be awesome to have it as a wakeup-light with colors and also have some tts or some other tune :wink:

Edit: I tested with a bluetooth headset (Sony SBH20), then your adress in HA-conf need to be
address: 00:2F:AD:12:0D:42.headset_head_unit, can be fixed in code but seems unnecessary.

Can be found with below, then you just test the sinkā€™s and chose one.
pactl list short | grep bluez
the test:
mplayer -ao pulse::bluez_sink.00_2F_AD_12_0D_42.headset_head_unit sometest.mp3

Is this solution raspbian only? Can I use it with my Hassio system?

1 Like

Dec 16 12:38:11 hassbian hass[1203]: 2017-12-16 12:38:11 INFO (Thread-9) [custom_components.media_player.tts_bluetooth_speaker] play_media: http://192.168.0.119:8123/api/tts_proxy/7d385d128cc4a59fa5ddb0211327c11b3f70f113_zh_-_baidu.mp3
Dec 16 12:38:11 hassbian hass[1203]: MPlayer 1.3.0 (Debian), built with gcc-6.2.1 (C) 2000-2016 MPlayer Team
Dec 16 12:38:11 hassbian hass[1203]: do_connect: could not connect to socket
Dec 16 12:38:11 hassbian hass[1203]: connect: No such file or directory
Dec 16 12:38:11 hassbian hass[1203]: Failed to open LIRC support. You will not be able to use your remote control.
Dec 16 12:38:11 hassbian hass[1203]: Terminal type `unknownā€™ is not defined.
Dec 16 12:38:11 hassbian hass[1203]: Playing /home/homeassistant/.homeassistant/tts/7d385d128cc4a59fa5ddb0211327c11b3f70f113_zh_-_baidu.mp3.
Dec 16 12:38:11 hassbian hass[1203]: libavformat version 57.56.101 (external)
Dec 16 12:38:11 hassbian hass[1203]: Mismatching header version 57.56.100
Dec 16 12:38:11 hassbian hass[1203]: Audio only file format detected.
Dec 16 12:38:11 hassbian hass[1203]: Load subtitles in /home/homeassistant/.homeassistant/tts/
Dec 16 12:38:11 hassbian hass[1203]: ==========================================================================
Dec 16 12:38:11 hassbian hass[1203]: Opening audio decoder: [mpg123] MPEG 1.0/2.0/2.5 layers I, II, III
Dec 16 12:38:11 hassbian hass[1203]: AUDIO: 16000 Hz, 2 ch, s16le, 16.0 kbit/3.12% (ratio: 2000->64000)
Dec 16 12:38:11 hassbian hass[1203]: Selected audio codec: [mpg123] afm: mpg123 (MPEG 1.0/2.0/2.5 layers I, II, III)
Dec 16 12:38:11 hassbian hass[1203]: ==========================================================================
Dec 16 12:38:11 hassbian hass[1203]: AO: [pulse] Init failed: No such entity
Dec 16 12:38:11 hassbian hass[1203]: Failed to initialize audio driver ā€˜pulse::bluez_sink.96_13_5A_00_4E_1B.a2dp_sinkā€™
Dec 16 12:38:11 hassbian hass[1203]: Could not open/initialize audio device ā†’ no sound.
Dec 16 12:38:11 hassbian hass[1203]: Audio: no sound
Dec 16 12:38:11 hassbian hass[1203]: Video: no video
Dec 16 12:38:11 hassbian hass[1203]: Exitingā€¦ (End of file)

I have this problem too,somebody can help me?

ā— pulseaudio.service - Pulse Audio
Loaded: loaded (/etc/systemd/system/pulseaudio.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2017-12-21 20:52:00 +07; 4min 28s ago
Main PID: 342 (pulseaudio)
CGroup: /system.slice/pulseaudio.service
ā””ā”€342 /usr/bin/pulseaudio --system --disallow-exit --disable-shm --exit-idle-time=-1

Dec 21 20:52:01 rha pulseaudio[342]: W: [pulseaudio] main.c: Running in system mode, but --disallow-module-loading not set.
Dec 21 20:52:01 rha pulseaudio[342]: W: [pulseaudio] main.c: OK, so you are running PA in system mode. Please make sure that you actually do want to do that.
Dec 21 20:52:01 rha pulseaudio[342]: W: [pulseaudio] main.c: Please read http://www.freedesktop.org/wiki/Software/PulseAudio/Documentation/User/WhatIsWrongWithSystemWide/
Dec 21 20:52:01 rha pulseaudio[342]: W: [pulseaudio] authkey.c: Failed to open cookie file ā€˜/var/run/pulse/.config/pulse/cookieā€™: No such file or directory
Dec 21 20:52:01 rha pulseaudio[342]: W: [pulseaudio] authkey.c: Failed to load authentication key ā€˜/var/run/pulse/.config/pulse/cookieā€™: No such file or directory
Dec 21 20:52:01 rha pulseaudio[342]: W: [pulseaudio] authkey.c: Failed to open cookie file ā€˜/var/run/pulse/.pulse-cookieā€™: No such file or directory
Dec 21 20:52:01 rha pulseaudio[342]: W: [pulseaudio] authkey.c: Failed to load authentication key ā€˜/var/run/pulse/.pulse-cookieā€™: No such file or directory
Dec 21 20:52:01 rha pulseaudio[342]: E: [pulseaudio] main.c: Failed to acquire org.pulseaudio.Server: org.freedesktop.DBus.Error.AccessDenied: Connection ā€œ:1.3ā€ is not al
Dec 21 20:52:05 rha pulseaudio[342]: E: [pulseaudio] bluez5-util.c: Found duplicated D-Bus path for adapter /org/bluez/hci0
Dec 21 20:52:05 rha pulseaudio[342]: E: [pulseaudio] bluez5-util.c: Found duplicated D-Bus path for device /org/bluez/hci0/dev_30_22_00_13_88_59

It connected the first time then after reboot like duplicate

pi@rha:~ $ pactl list short | grep bluez
11 module-bluez5-discover