I cannot change the pulse audio output to headphones from commandline


From commandline I can easily change the audio output, eg:
pactl set-card-profile 0 output:hdmi-stereo-extra2
pactl set-card-profile 0 output:hdmi-stereo
and restore:
pactl set-card-profile 0 output:analog-stereo

But I’m not able to change the output to the headphone.
I can only do it on GUI by using pavucontrol.

What I’ve tried to do:

pactl set-card-profile 0 output:analog-output-headphones
pactl set-card-profile 0 output:headphones
pactl set-card-profile 0 output:stereo-headphones
pactl set-card-profile 0 output:analog-headphones

I always get Failure: No such entity.

pacmd list-sinks, however gave me:
analog-output-headphones: Headphones

But nothing which I tried works.
Can someone tell me what is wrong here and how can I set the headphone output from commandline?

I solved :slight_smile:

This is how I achieved this needs:


SINKID=$(pacmd list-sinks | sed -n -e '3{p;q}' | awk '{ print $2 }' | tr -d '><')
ACTIVESINK=$(pacmd list-sinks | grep 'active port' | awk '{ print $3 }' | tr -d '><')

if [ "$ACTIVESINK" == "analog-output-headphones" ]; then
	pacmd set-sink-port $SINKID analog-output-speaker

if [ "$ACTIVESINK" == "analog-output-speaker" ]; then
	pacmd set-sink-port $SINKID analog-output-headphones

exit 0

If is active the output speaker, this script switch it to headphones; if the output is set to headphones, it switch back to speaker.

Let me explain why I needed this.
Obviously, when I plug in the headphones, the audio output is automatically switched to headphones.
So, why I need to do it manually?

Well: I installed SoundWire app on one Android Phone, and on Manjaro, its counterpart: SoundWire Server, so I can send any audio which I’m listen on Linux, to the Android phone.
But there is an annoying thing: when I transmit the audio to the phone, at the same time is reproduced on the laptop’s speakers. Nope: is not enough muting the audio, because is also muted on Android; the only way to stop the audio output on laptop when is reproduced on the phone, was to plug in the headphones, but maybe I need them fom something else.

So I noticed that if in Pavucontrol I set the audio output to headphones (also if they are not present) I achieved the same result.

Now I launch SoundWire Server on Manjaro, using the following script:


SINKID=$(pacmd list-sinks | sed -n -e '3{p;q}' | awk '{ print $2 }' | tr -d '><')
pacmd set-sink-port $SINKID analog-output-headphones

SoundWireServer &
echo $! > /tmp/sndwire
pid=$(cat /tmp/sndwire)
### Wait for process termination ###
tail --pid=$pid -f /dev/null

### process is ended: run the post actions ###
pacmd set-sink-port $SINKID analog-output-speaker

As I start SoundWire Server the output is switched to headphones, and as I quit it, the audio output is restored to laptop’s speakers :slight_smile:

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.