raspberry-pipulseaudio

Dual Audio output to both Pulseaudio Bluetooth and HDMI on Raspberry Pi 3B+ Buster


Audio output to both Pulseaudio and HDMI?

On my boat the raspberry pi 3 B+ Buster, is used in two ways:

  1. it runs Kodi to play music that outputs via bluetooth to a car radio that outputs to speakers. (it took two days of work to get that to happen finally found https://peppe8o.com/fixed-connect-bluetooth-headphones-with-your-raspberry-pi/)
  2. it also plays movies that output to an HDMI projector with speakers.

The Pi boots up into LXDE which runs a bash script to connect (and to keep trying to connect as per BluManCZ's answer in https://unix.stackexchange.com/questions/334386/how-to-set-up-automatic-connection-of-bluetooth-headset) to the radio by bluetooth and then autostarts Kodi.

The music is controlled by an Android Yatse app.

When I want to play a movie, I stop the music playing with the Yatse app and then turn on the projector and use a wireless keyboard to play a movie. But I have to manually go to Kodi system settings and select HDMI as the sound ouput.

When I finish the movie, I power off the Pi correctly. But if I forget to first go and manually put the sound output back to Pulseaudio within Kodi then when I next boot it up, and expect to get music, I hear nothing, as it is still going to HDMI. So then I have to go and turn on the projector so I can use the keyboard to switch it back over to pulseaudio.

So, is there some way I can get it to either output to BOTH pulseaudio (bluetooth) AND HDMI so that whichever device is switched on (radio or projector) I get sound out?

Or can I have it automatically detect which one is active and output to that?

How do I get it so that I can seamlessly switch from playing music through the bluetooth to playing videos through the projector?

Bear in mind that when I power up the Pi either one, or other, or both the radio and projector might be powered on at that time.


Solution

  • Ok, I solved it, I think, using the following steps. This asumes that bluetooth output to the a2dp speakers works (see the steps I took for that in the original question):

    Install paprefs:

    $ sudo apt install paprefs
    

    Then run it on the desktop using Terminal:

    $ paprefs
    

    This brings up a GUI window with several tabs. Select the 'Simultaneour Out Put' tab which offers one checkbox to enable or disable the feature. Turn it on.

    Then restart pulseaudio

    $ killall pulseaudio
    

    Now you can go to VLC and select the audio tab to send the output to Simultaneous output. The sound will go to both. Unfortunately when you shut down VLC it goes back to HDMI and you have to manually change it again. Also in Kodi it does not appear as an option.

    So, a few more steps:

    Edit the default configuration for pulseaudio

    $ sudo nano /etc/pulse/default.pa
    

    and add the following line at the beginning, before any other modules are loaded:

    load-module module-combine-sink sink_name=combined
    

    This sets up a new virtual device that outputs to all the others.

    While you are there, make sure that this line is also in there, somewhere (probably farther down)

    module-default-device-restore
    

    This will revert back the the default device if something changes in the system (eg HDMI is turned off or on).

    Exit nano and save the file by doign ctrl-x and saying yes to the prompts.

    List the available devices known to pulseaudio:

    $ pacmd list-sinks | grep -e 'name:' -e 'index:'
    

    This should now list the bluetooth, jack, HDMI and also combined devices. The one with the asterix is the current fall-back device. You want to make this the 'combined' sink. To do that:

    $ sudo pacmd set-default-sink combined
    
    
    $ sudo reboot 0
    

    Check again, and this time the combined should have the asterix next to it:

    $ pacmd list-sinks | grep -e 'name:' -e 'index:'
    

    Now when you play VLC or Kodi the sound should go to both HDMI and Bluetooth.

    This seems to survive a full shutdown and power up, so I think it achieves the goal. I have not yet tried all the different combinations of starting with the different output devices on or off, But I am hopefull that it works.