I want to be able to change the audio output device (hdmi or aux) from the command line, preferably, without having to sudo
. I'm on a RPI4 with Raspbian-lite Buster.
On the RPI3 I was able to use Omxplayer to change the audio output device but Omxplayer does not support the RPI4 and we are told to migrate to using VLC. VLC doesn't provide this functionality, as far as I know, and I don't want to sudo raspi-config
every time I want to change the output.
aplay -l
gives me..
**** List of PLAYBACK Hardware Devices ****
card 0: b1 [bcm2835 HDMI 1], device 0: bcm2835 HDMI 1 [bcm2835 HDMI 1]
Subdevices: 4/4
Subdevice #0: subdevice #0
Subdevice #1: subdevice #1
Subdevice #2: subdevice #2
Subdevice #3: subdevice #3
card 1: Headphones [bcm2835 Headphones], device 0: bcm2835 Headphones [bcm2835 Headphones]
Subdevices: 4/4
Subdevice #0: subdevice #0
Subdevice #1: subdevice #1
Subdevice #2: subdevice #2
Subdevice #3: subdevice #3
amixer cget numid=2
gives me..
numid=2,iface=MIXER,name='Headphone Playback Switch'
; type=BOOLEAN,access=rw------,values=1
: values=on
I can turn the audio to headphones off with amixer cset numid=2 0
but then I still get no sound from hdmi.
Edit:
I am able to select the two cards with -c N
flag. So I can turn off the HDMI card by amixer -c 0 cset numid=2 0
.
numid=2,iface=MIXER,name='HDMI Playback Switch'
; type=BOOLEAN,access=rw------,values=1
: values=off
I can turn on Headphone audio with amixer -c 1 cset numid=2 1
.
numid=2,iface=MIXER,name='Headphone Playback Switch'
; type=BOOLEAN,access=rw------,values=1
: values=on
However, this has no effect. I cannot hear audio out of headphones. It still only works if I change manually in sudo raspi-config
. I can only use the amixer commands to turn off or on the device that is already selected in raspi-config.
Edit:
I think it has to do with the ~/.asoundrc
file. If I own the file sudo chown myusername:myusername ~/.asoundrc
and then edit the card
value to 0
(hdmi) or 1
(headphones) in the following section.
pcm.output {
type hw
card 1
}
Changes to this file is reflected in the sound output from both devices. I can find a way to edit this file automatically from the command line I guess but I wish there was a cleaner way. I'm also getting a lot of popping and hissing in my headphone audio like I do if I try and use Omxplayer.