pulseaudio: auto switch sink when headphones connected











up vote
2
down vote

favorite
1












I'm running XUbuntu 16.04. Previously I had speakers connected to analog line-out, and sound would automatically switch over to headphones when they were plugged in. This was all happening on one output device (motherboard audio).



I've now got a new monitor with built-in speakers, but no analog connection, so I'm getting audio over DisplayPort from my Radeon R9 270. I'd like pulseaudio to switch existing streams and new streams to the headphone port on the motherboard audio when I plug in the headphones, and back to the DP audio port on the GPU device when I unplug them again.



I've tried using pactl load-module module-switch-on-connect, but it doesn't seem to have any effect, presumably because plugging in the headphones doesn't create a new sink, only a new port.



I know from reading other questions that I can do this manually with pactl/pacmd invocation and I'll do that if I have to, but I'd much prefer to have this happen automatically. I don't mind if I have to do some scripting to do it, but is there a hook I can intercept to get a call when the headphones are connected or disconnected?










share|improve this question














bumped to the homepage by Community 2 days ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.



















    up vote
    2
    down vote

    favorite
    1












    I'm running XUbuntu 16.04. Previously I had speakers connected to analog line-out, and sound would automatically switch over to headphones when they were plugged in. This was all happening on one output device (motherboard audio).



    I've now got a new monitor with built-in speakers, but no analog connection, so I'm getting audio over DisplayPort from my Radeon R9 270. I'd like pulseaudio to switch existing streams and new streams to the headphone port on the motherboard audio when I plug in the headphones, and back to the DP audio port on the GPU device when I unplug them again.



    I've tried using pactl load-module module-switch-on-connect, but it doesn't seem to have any effect, presumably because plugging in the headphones doesn't create a new sink, only a new port.



    I know from reading other questions that I can do this manually with pactl/pacmd invocation and I'll do that if I have to, but I'd much prefer to have this happen automatically. I don't mind if I have to do some scripting to do it, but is there a hook I can intercept to get a call when the headphones are connected or disconnected?










    share|improve this question














    bumped to the homepage by Community 2 days ago


    This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.

















      up vote
      2
      down vote

      favorite
      1









      up vote
      2
      down vote

      favorite
      1






      1





      I'm running XUbuntu 16.04. Previously I had speakers connected to analog line-out, and sound would automatically switch over to headphones when they were plugged in. This was all happening on one output device (motherboard audio).



      I've now got a new monitor with built-in speakers, but no analog connection, so I'm getting audio over DisplayPort from my Radeon R9 270. I'd like pulseaudio to switch existing streams and new streams to the headphone port on the motherboard audio when I plug in the headphones, and back to the DP audio port on the GPU device when I unplug them again.



      I've tried using pactl load-module module-switch-on-connect, but it doesn't seem to have any effect, presumably because plugging in the headphones doesn't create a new sink, only a new port.



      I know from reading other questions that I can do this manually with pactl/pacmd invocation and I'll do that if I have to, but I'd much prefer to have this happen automatically. I don't mind if I have to do some scripting to do it, but is there a hook I can intercept to get a call when the headphones are connected or disconnected?










      share|improve this question













      I'm running XUbuntu 16.04. Previously I had speakers connected to analog line-out, and sound would automatically switch over to headphones when they were plugged in. This was all happening on one output device (motherboard audio).



      I've now got a new monitor with built-in speakers, but no analog connection, so I'm getting audio over DisplayPort from my Radeon R9 270. I'd like pulseaudio to switch existing streams and new streams to the headphone port on the motherboard audio when I plug in the headphones, and back to the DP audio port on the GPU device when I unplug them again.



      I've tried using pactl load-module module-switch-on-connect, but it doesn't seem to have any effect, presumably because plugging in the headphones doesn't create a new sink, only a new port.



      I know from reading other questions that I can do this manually with pactl/pacmd invocation and I'll do that if I have to, but I'd much prefer to have this happen automatically. I don't mind if I have to do some scripting to do it, but is there a hook I can intercept to get a call when the headphones are connected or disconnected?







      pulseaudio






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Feb 4 '17 at 21:11









      Bruce Merry

      1263




      1263





      bumped to the homepage by Community 2 days ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







      bumped to the homepage by Community 2 days ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          I figured out a solution thanks to this discussion and paswitch. In summary, acpid can be used to hook the headphone plug/unplug events.



          I created the following files:



          /etc/acpi/events/headphone-jack:



          event=jack/headphone
          action=/etc/acpi/headphone-jack.sh %e


          /etc/acpi/headphone-jack.sh (remember to make it executable):



          #!/bin/bash
          set -e -u

          if [ "$1" = "jack/headphone" -a "$2" = "HEADPHONE" ]; then
          case "$3" in
          plug)
          sink=alsa_output.pci-0000_00_1b.0.analog-stereo
          ;;
          *)
          sink=alsa_output.pci-0000_01_00.1.hdmi-stereo
          ;;
          esac
          for userdir in /run/user/*; do
          uid="$(basename $userdir)"
          user="$(id -un $uid)"
          if [ -f "$userdir/pulse/pid" ]; then
          PULSE_RUNTIME_PATH="$userdir/pulse" su "$user" -c "paswitch $sink"
          fi
          done
          fi


          The sinks naturally need to be updated depending on your system.



          I'm not sure how robust the script is to an actual multi-user system, but it works for me.






          share|improve this answer





















            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "106"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f342556%2fpulseaudio-auto-switch-sink-when-headphones-connected%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            I figured out a solution thanks to this discussion and paswitch. In summary, acpid can be used to hook the headphone plug/unplug events.



            I created the following files:



            /etc/acpi/events/headphone-jack:



            event=jack/headphone
            action=/etc/acpi/headphone-jack.sh %e


            /etc/acpi/headphone-jack.sh (remember to make it executable):



            #!/bin/bash
            set -e -u

            if [ "$1" = "jack/headphone" -a "$2" = "HEADPHONE" ]; then
            case "$3" in
            plug)
            sink=alsa_output.pci-0000_00_1b.0.analog-stereo
            ;;
            *)
            sink=alsa_output.pci-0000_01_00.1.hdmi-stereo
            ;;
            esac
            for userdir in /run/user/*; do
            uid="$(basename $userdir)"
            user="$(id -un $uid)"
            if [ -f "$userdir/pulse/pid" ]; then
            PULSE_RUNTIME_PATH="$userdir/pulse" su "$user" -c "paswitch $sink"
            fi
            done
            fi


            The sinks naturally need to be updated depending on your system.



            I'm not sure how robust the script is to an actual multi-user system, but it works for me.






            share|improve this answer

























              up vote
              0
              down vote













              I figured out a solution thanks to this discussion and paswitch. In summary, acpid can be used to hook the headphone plug/unplug events.



              I created the following files:



              /etc/acpi/events/headphone-jack:



              event=jack/headphone
              action=/etc/acpi/headphone-jack.sh %e


              /etc/acpi/headphone-jack.sh (remember to make it executable):



              #!/bin/bash
              set -e -u

              if [ "$1" = "jack/headphone" -a "$2" = "HEADPHONE" ]; then
              case "$3" in
              plug)
              sink=alsa_output.pci-0000_00_1b.0.analog-stereo
              ;;
              *)
              sink=alsa_output.pci-0000_01_00.1.hdmi-stereo
              ;;
              esac
              for userdir in /run/user/*; do
              uid="$(basename $userdir)"
              user="$(id -un $uid)"
              if [ -f "$userdir/pulse/pid" ]; then
              PULSE_RUNTIME_PATH="$userdir/pulse" su "$user" -c "paswitch $sink"
              fi
              done
              fi


              The sinks naturally need to be updated depending on your system.



              I'm not sure how robust the script is to an actual multi-user system, but it works for me.






              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                I figured out a solution thanks to this discussion and paswitch. In summary, acpid can be used to hook the headphone plug/unplug events.



                I created the following files:



                /etc/acpi/events/headphone-jack:



                event=jack/headphone
                action=/etc/acpi/headphone-jack.sh %e


                /etc/acpi/headphone-jack.sh (remember to make it executable):



                #!/bin/bash
                set -e -u

                if [ "$1" = "jack/headphone" -a "$2" = "HEADPHONE" ]; then
                case "$3" in
                plug)
                sink=alsa_output.pci-0000_00_1b.0.analog-stereo
                ;;
                *)
                sink=alsa_output.pci-0000_01_00.1.hdmi-stereo
                ;;
                esac
                for userdir in /run/user/*; do
                uid="$(basename $userdir)"
                user="$(id -un $uid)"
                if [ -f "$userdir/pulse/pid" ]; then
                PULSE_RUNTIME_PATH="$userdir/pulse" su "$user" -c "paswitch $sink"
                fi
                done
                fi


                The sinks naturally need to be updated depending on your system.



                I'm not sure how robust the script is to an actual multi-user system, but it works for me.






                share|improve this answer












                I figured out a solution thanks to this discussion and paswitch. In summary, acpid can be used to hook the headphone plug/unplug events.



                I created the following files:



                /etc/acpi/events/headphone-jack:



                event=jack/headphone
                action=/etc/acpi/headphone-jack.sh %e


                /etc/acpi/headphone-jack.sh (remember to make it executable):



                #!/bin/bash
                set -e -u

                if [ "$1" = "jack/headphone" -a "$2" = "HEADPHONE" ]; then
                case "$3" in
                plug)
                sink=alsa_output.pci-0000_00_1b.0.analog-stereo
                ;;
                *)
                sink=alsa_output.pci-0000_01_00.1.hdmi-stereo
                ;;
                esac
                for userdir in /run/user/*; do
                uid="$(basename $userdir)"
                user="$(id -un $uid)"
                if [ -f "$userdir/pulse/pid" ]; then
                PULSE_RUNTIME_PATH="$userdir/pulse" su "$user" -c "paswitch $sink"
                fi
                done
                fi


                The sinks naturally need to be updated depending on your system.



                I'm not sure how robust the script is to an actual multi-user system, but it works for me.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Feb 4 '17 at 21:52









                Bruce Merry

                1263




                1263






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f342556%2fpulseaudio-auto-switch-sink-when-headphones-connected%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Entries order in /etc/network/interfaces

                    新発田市

                    Grub takes very long (several minutes) to open Menu (in Multi-Boot-System)