Switch audio outport on keypress

  • Hey guys,

    I am pretty new to scripting on a Linux based system so my thoughts are kind of blank about any command I have to work with. I already tried to find something similar using the forums search and google but that was not successful. Maybe you guys can help me.

    I have a Raspberry Pie 3 and it is running Kodi / LibreELEC. The device is connected to my TV via HDMI.

    There are two use cases that I would like to make easily accessible:
    1. Watching a movie / TV Show. In this case I want the Pi to use HDMI as audio outport so I hear the sound from the 5.1 system (which is connected to the TV).
    2. Listening to music. In that case I want to connect a Bluetooth speaker to the Pi and make it use Blutetooth as audio outport.

    I know I can do this in the settings, but it is a little inconvenient. Also, some sort of a one-click-solution would even make it usable for my wife and my son ;)
    That's why I thought of a script.

    I want a script to be started when I hit a specific key on the remote control (CEC). Therefore I created a file named keymap.xml in /storage/.kodi/userdata/keymaps/. It contains the following code:

    Code
    <keymap>
       <global>
          <remote>
             <!-- 207 = "0" @ remote control -->
             <key id="207">RunScript("/storage/scripts/AudioOut_trigger.sh")</key>
          </remote>
       </global>
    </keymap>


    This should be working I guess...
    The *.sh file doesn't contain anything yet. As I wrote above I have no specific idea about which commands to use. I am not even sure if it makes the most sense to write a shell script or to better do it with python or something. It's probably a matter of taste or belief.

    What I know is what the script should do (in pseudo code):


    Can you guys help me to realize that idea?

    Thank you all in advance!

  • Unfortunately the AddOn doesn't do the trick. It lets you create profiles for different use cases but these profiles only contain "volume level", "player setings" and "video settings". There's no way to make it change the audio outport.

    I'll probably use it additionally to avoid the sound being very loud via HDMI compared to when the Bluetooth speaker is used because the have different levels of amplification.

    If there's no other AddOn I don't know about, can anyone give me some hints implementing a script that does what I plan to have?

    Thanks a lot!
    Till


  • you can try this one, but i don't know if it works with bluetooth speaker:
    AUTOMATIC (and/or manual) AUDIO SWITCHER for different Audio Output

    Thanks guys... I try to get this working for me. It'll probably take a couple of days due to my python skills and christmas of course. I'm going to give you an update when there's something to say ;)

    I can't say what went wrong the first times I used search... Maybe a layer 8 issue :P
    Now I even find more e.g. this one looks promising: http://forum.kodi.tv/showthread.php?tid=199579&highlight=switch+audio. I'll put that to the test, too.

  • Hey guys,

    wow, a lot of time passed... But I also got a lot closer to the solution for my plan. I have an active keymapping that triggers a python script which switches between two sets of audio settings. It changes the output device the number of channels and the passthrough state.

    keymap.xml (my remote is recognized as a keyboard):

    Code
    <keymap>
        <global>
            <keyboard name="Samsung Remote">
                <!-- 207 = "0" @ remote control -->
                <key id="207">RunScript(special://profile/user_scripts/audioSwitch.py)</key>
            </keyboard>
        </global>
    </keymap>

    audioSwitch.py (based on teeedubb's script from here:(

    That works just fine (you can also see the chhanges in the settings menu, too) and I am very happy about it.

    What's left to do is to force connect the BT speaker which should be already paired and trusted (I don't think it is necessary to script pairing and trusting, too).
    Therefore I want a message window to tell the user to turn on the speaker an when it's turned on the user has to klick "OK" or "Turned on" or something like that). A "Cancel" button should stop the whole process.
    After "OK" was pressed, a command will be sent to connect the speaker using it's MAC address, e.g.

    Code
    bluetoothctl
    connect 88:C6:26:50:63:B6
    exit


    when unsing ssh (as found here and here). I don't know how to do this in python and / or via json (yet?).
    The response of that command should be visible for the user via notification (e.g. "BT speaker connected" / "Could not connect BT speaker").

    I would insert this procedure before setting the audio output device to PULSE:Default (BT) and only change the output settings if the user didn't press "Cancel" and the speaker was successfully connected to the Raspberry Pi 3.

    I'll try to figure out how to implement all this and keep the thread up to date.
    Anyways, if anyone of you has help to offer I'll appreciate it.