Switching Audio Output with a Button - Raspberry Pi Forums


i building snes in box 10" hdmi screen. unfortunately screen has no audio have set default audio 3.5mm jack using raspi-config , have small speaker connected.

have hdmi splitter goes female hdmi port in side of box in case want plug system tv. in instance want disable 3.5mm jack , push audio through hdmi. unfortunately, both screens run of hdmi, auto switching won't work. therefore including momentary push button allow audio output switched easily. know in retropie menu plan on removing menu item once setup correctly friends mess up. button quicker , easier going through menu system.

below work? i've read os.system isn't preferred command anymore should change , if should python script below (that added rc.local) altered to?

edit: below script updated , works me. if have issues due spacing of lines. default audio set speaker , if plug in hdmi press momentary button switch swap hdmi out.

thanks!

code: select all

import rpi.gpio gpio import time import subprocess import os  gpio.setmode(gpio.bcm) gpio.setup(18, gpio.in, pull_up_down=gpio.pud_up) set = 0 while true:     input_state = gpio.input(18)     if input_state == false , set = 0:         os.system("amixer -c 0 cset numid=3 1")          time.sleep(1)         set =1     input_state = gpio.input(18)     if input_state == false , set = 1:          os.system("amixer -c 0 cset numid=3 2")          time.sleep(1)          set =0

i had add following config.txt prevent audio being awful (white noise):

code: select all

disable_audio_dither=1
unfortunately hdmi splitter caused prevented image appearing on hdmi cctv screen i've fit in box have had remove splitter time being until figure out going wrong. before pulled tried boosting hdmi signal no success. makes whole audio out switch bit redundant until can fix this.


raspberrypi



Comments