import RPi.GPIO as GPIO
import os
GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(5, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(6, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(7, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(8, GPIO.IN, pull_up_down=GPIO.PUD_UP)
while True:
input_state = GPIO.input(4)
if input_state == False:
print('Tutorial')
os.system('killall mpg123')
os.system('mpg123 -q audio1.mp3 &')
time.sleep(0.2)
input_state = GPIO.input(5)
if input_state == False:
print('I Regíon')
os.system('killall mpg123')
os.system('mpg123 -q audio2.mp3 &')
time.sleep(0.2)
input_state = GPIO.input(6)
if input_state == False:
print('II Regíon')
os.system('killall mpg123')
os.system('mpg123 -q audio3.mp3 &')
time.sleep(0.2)
input_state = GPIO.input(7)
if input_state == False:
print('XI Regíon')
os.system('killall mpg123')
os.system('mpg123 -q audio4.mp3 &')
time.sleep(0.2)
input_state = GPIO.input(8)
if input_state == False:
print('X Regíon')
os.system('killall mpg123')
os.system('mpg123 -q audio5.mp3 &')
time.sleep(0.2)
这会将5个音频分配给按下时播放的按钮。从这里一切都很好,但这是我的问题/问题。 我有5个带有指定音频的按钮,所以我想知道是否可以在按下另一个按钮时将另外5个音频分配给相同的按钮(我希望你理解)。为了解释这一点,我有这个图像:
这个想法是通过按下" 1"按钮你可以播放a1b1c1d1e1声音(对应于分配给声音的5个按钮) 按下" 2"按钮你可以播放a2b2c2d2e2声音等等,直到" 5"按钮。
如果有人知道某些方法可以在" easy"方式会很棒(除了我已经做过的事情之外,我对python知之甚少)
非常感谢,并为我糟糕的英语感到抱歉
答案 0 :(得分:0)
我没有完全按照您的要求进行操作,但我理解为想要更改与所有按钮相关的声音,而不是说数字' 1'按下,例如按下' 2'会分配声音' a2'按钮' a。为此,在while循环外部放置一个变量,指示应该使用的声音集,以及包含该按钮所需声音集的5个音频按钮中的每一个的数组。例如,假设你默认为a1b1c1d1e1,那么就像这样:
sound_index = 0 # corresponds to sound set 1
a_sound_files = ['a1.mp3', 'a2.mp3', ...]
...
while true:
input_state = GPIO.input(4)
if input_state == False:
print('Tutorial')
os.system('killall mpg123')
audio_file = a_sound_files[sound_index]
os.system('mpg123 -q {file} &'.format(file=audio_file))
time.sleep(0.2)
...
...
...
然后只需添加5个新按钮的大小写,并让它们相应地更改声音索引。
修改强> 代码如下:
import RPi.GPIO as GPIO
import os
GPIO.setmode(GPIO.BCM)
# I'm assuming 4 is 'a', 5 is 'b', and so on
GPIO.setup(4, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(5, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(6, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(7, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(8, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# Let these be your new buttons where 11 = button 1,
# 12 = button 2, and so on
GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(12, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(13, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(14, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(15, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# Keeps track of which sound set to play
sound_index = 0 # corresponds to sound set 1
# These contain the set of sounds for each button
a_sound_files = ['a1.mp3', 'a2.mp3', ...]
b_sound_files = ['b1.mp3', 'b2.mp3', ...]
c_sound_files = ['c1.mp3', 'c2.mp3', ...]
d_sound_files = ['d1.mp3', 'd2.mp3', ...]
e_sound_files = ['e1.mp3', 'e2.mp3', ...]
while True:
input_state = GPIO.input(4)
if input_state == False:
print('Tutorial')
os.system('killall mpg123')
audio_file = a_sound_files[sound_index]
os.system('mpg123 -q {file} &'.format(file=audio_file))
time.sleep(0.2)
input_state = GPIO.input(5)
if input_state == False:
print('I Regíon')
os.system('killall mpg123')
audio_file = b_sound_files[sound_index]
os.system('mpg123 -q {file} &'.format(file=audio_file))
time.sleep(0.2)
input_state = GPIO.input(6)
if input_state == False:
print('II Regíon')
os.system('killall mpg123')
audio_file = c_sound_files[sound_index]
os.system('mpg123 -q {file} &'.format(file=audio_file))
time.sleep(0.2)
input_state = GPIO.input(7)
if input_state == False:
print('XI Regíon')
os.system('killall mpg123')
audio_file = d_sound_files[sound_index]
os.system('mpg123 -q {file} &'.format(file=audio_file))
time.sleep(0.2)
input_state = GPIO.input(8)
if input_state == False:
print('X Regíon')
os.system('killall mpg123')
audio_file = e_sound_files[sound_index]
os.system('mpg123 -q {file} &'.format(file=audio_file))
time.sleep(0.2)
# These are your buttons that change the sound set
input_state = GPIO.input(11)
if input_state == False:
sound_index = 0
time.sleep(0.2)
input_state = GPIO.input(12)
if input_state == False:
sound_index = 1
time.sleep(0.2)
input_state = GPIO.input(13)
if input_state == False:
sound_index = 2
time.sleep(0.2)
input_state = GPIO.input(14)
if input_state == False:
sound_index = 3
time.sleep(0.2)
input_state = GPIO.input(15)
if input_state == False:
sound_index = 4
time.sleep(0.2)
这个问题有很多解决方案。 Python是一种相当直接的语言,所以一旦你想出了一个通用的问题解决方案,语法不应该限制你。