Steps

1. Install PulseAudio and bluetooth module:

sudo apt-get install pulseaudio
sudo apt-get install pavucontrol paprefs
sudo apt-get install pulseaudio-module-bluetooth

 

2. Connect bluetooth speaker and configure:

bluetoothctl
power on
agent on
default-agent
quit
sudo killall bluealsa
pulseaudio -k
pulseaudio –start
pactl unload-module module-bluetooth-discover
pactl load-module module-bluetooth-discover

 

3. Turn your bluetooth speaker on and disconnect from other devices:

bluetoothctl
scan on

 

4. Wait until Raspberry Pi finds the target bluetooth speaker, and note the MAC address such as “XX:XX:XX:XX:XX:XX”:

scan off

 

5. Replace “XX:XX:XX:XX:XX:XX” with your address:

pair XX:XX:XX:XX:XX:XX
trust XX:XX:XX:XX:XX:XX
connect XX:XX:XX:XX:XX:XX

 

6. Confirm it is recognized as a sound device:

pacmd list-cards

 

7. You should see your device address like “XX:XX:XX:XX:XX:XX” somewhere:

pacmd set-card-profile bluez_card.XX_XX_XX_XX_XX_XX a2dp_sink
pacmd set-default-sink bluez_sink.XX_XX_XX_XX_XX_XX.a2dp_sink

 

8. Edit “start-pulseaudio-x11” script to connect bluetooth speaker on startup:

sudo nano /usr/bin/start-pulseaudio-x11

 

9. Find the block of commands below:

if [ x”$SESSION_MANAGER” != x ] ; then
/usr/bin/pactl load-module module-x11-xsmp “display=$DISPLAY session_manager=$SESSION_MANAGER” > /dev/null
fi

 

10. Add the following lines of commands right after it (this will let you get sound from your speaker):

/usr/bin/pactl unload-module module-bluetooth-discover
/usr/bin/pactl load-module module-bluetooth-discover
sudo killall bluealsa
pulseaudio –start
bluetoothctl << EOF
connect XX:XX:XX:XX:XX:XX
EOF

 

11. Save the file by pressing Ctrl + X then Y and finally ENTER.

12. Modify “.bashrc” file.

sudo nano ~/.bashrc

 

13. Add the following lines of commands at the bottom:

/usr/bin/pactl unload-module module-bluetooth-discover
/usr/bin/pactl load-module module-bluetooth-discover
sudo killall bluealsa
pulseaudio –start
bluetoothctl << EOF
connect XX:XX:XX:XX:XX:XX
EOF

 

14. Save the file by pressing Ctrl + X then Y and finally ENTER. Then restart the Raspberry Pi:

sudo reboot

 

Credits

https://www.funincomplete.com/how-to-make-raspberry-pi-speak/