0

I am working on a pi project which uses audio out through HDMI. I am using respeaker mic for audio in. My problem is everytime when I startup the pi, the default audio is analog. I need to change it to HDMI by logging in and running

amixer -c 0 cset numid=3 2

using the terminal. I tried adding this line in a file sound.sh and when i execute the shell script using terminal it executes fine and changes to hdmi audio. My question is how to add this shell script to startup of pi, so that i dont have to run the script manually. i tried adding it to /etc/rc.local file. But doesnt seem to work. I am not sure of the exact command too. i tried:

sudo sh /home/pi/sound.sh &
sudo /home/pi/sound.sh &
/home/pi/sound.sh &
sh /home/pi/sound.sh &

Please help me out.

oh.dae.su
9341 gold badge5 silver badges12 bronze badges
asked Dec 3, 2018 at 7:53
1
  • You probably need to add the full path to 'amixer' in your sound.sh script. Commented Dec 3, 2018 at 8:51

2 Answers 2

2

Raspbian comes with systemd and old style SysV rc.local is only emulated by it. It is known that it has limitations and will be supported less step by step. The developer write in Compatibility with SysV:

In general, it's a good idea to write proper unit files with properly defined dependncies, and avoid making use of rc.local.

Here is a simple Unit file that may work. If not, tell it. Create it with:

rpi ~$ sudo systemctl edit --force set_audio.service

In the empty editor insert these statements, save them and quit the editor:

[Unit]
Description=Switch audio out to HDMI
After=multi-user.target
[Service]
Type=oneshot
ExecStart=/usr/bin/amixer cset numid=3 2
[Install]
WantedBy=multi-user.target

Enable the new service with:

rpi ~$ sudo systemctl enable set_audio.service

According to Audio configuration you also may have to set hdmi_drive=2 in /boot/config.txt.

answered Dec 3, 2018 at 13:49
1

Add your amixer -c 0 cset numid=3 2 a line to /etc/rc.local (just before the exit 0 line).

That gets run once at boot time.

If you add it as amixer -c 0 cset numid=3 2 > /tmp/amixer.out 2> /tmp/amixer.err you'll be able to look at those files to see any error messages that may be issued by your command.

answered Dec 3, 2018 at 11:59

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.