我只是想做一件非常简单的事情,我想根据触摸传感器播放两个不同的声音文件。我目前遇到的问题是,尽管传感器正常工作,但程序不会更改音频文件……所以,我现在一无所知,有什么帮助吗?
提前致谢,
萝拉
import pygame
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7, GPIO.IN)
pygame.mixer.pre_init(44100, -16, 12, 512)
pygame.mixer.init()
pygame.init()
fastbeat = pygame.mixer.Sound('/home/pi/gpio-music-box/samples/fastbeat.ogg')
fastbeat.set_volume(.4);
slowbeat = pygame.mixer.Sound('/home/pi/gpio-music-box/samples/slowbeat.ogg')
slowbeat.set_volume(.4)
while True:
if(GPIO.input(7)): #interrupt pin is high
print ('touched')
fastbeat.play()
else: # Interupt pin is low
print ('not touched')
slowbeat.play()
我现在将代码更改为:
import pygame
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7, GPIO.IN)
pygame.mixer.pre_init(44100, -16, 12, 512)
pygame.mixer.init()
pygame.init()
fastbeat = pygame.mixer.Sound('/home/pi/gpio-music-box/samples/fastbeat.ogg')
fastbeat.set_volume(.4);
slowbeat = pygame.mixer.Sound('/home/pi/gpio-music-box/samples/slowbeat.ogg')
slowbeat.set_volume(.4)
while True:
if(GPIO.input(7)): #interrupt pin is high
print ('touched')
slowbeat.stop()
fastbeat.play()
else: # Interupt pin is low
print ('not touched')
fastbeat.stop()
slowbeat.play()
并且它对我有用,但是另一个问题是,有什么办法可以更改if -command,使其只播放一次快速拍子,然后再切换回慢拍?
问候萝拉
最佳答案
看来您是在无限循环中播放慢拍-因此它没有时间让音频开始播放。
试试这个?
isButtonUp = true #was the button untouched?
while True:
if(GPIO.input(7)): #interrupt pin is high
isButtonUp = false #button is not untouched
print ('touched')
fastbeat.play()
else if (!isButtonUp): #has button been untouched before?
print ('not touched')
isButtonUp=true #set button as touched
slowbeat.play()
关于python - 使用触摸传感器在两个音频文件之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50706795/