你好,我是 python 的新手,尝试在按钮上播放多个视频。(一位设计师试图用 raspberry 3 b 进行安装)。我有一个与事件检测和回调函数一起使用的“mainloop”。那部分有效。在我的函数 playVid1() 中,我尝试用我的视频打开 omxplayer。第一次它工作得很好,预计它似乎没有正确关闭播放器并且不会在循环中返回。当我再次单击该按钮时,播放器再次打开,但不再播放我的视频。如果有人可以帮助我,我会非常高兴。有什么提示吗?谢谢和最好的问候
import RPi.GPIO as GPIO
import time
import os
import sys
from subprocess import Popen
#pins on raspy
button1 = 23
button2 = 21
button3 = 19
button4 = 15
button5 = 13
button6 = 11
button7 = 7
button8 = 29
button9 = 31
led1 = 36
led2 = 26
led3 = 24
led4 = 22
led5 = 18
led6 = 16
led7 = 12
led8 = 38
led9 = 40
movie1 = ("/home/pi/Videos/video1.mp4")
video1_length = 10.0 # seconds before led turns off and
player = False
def setup():
GPIO.setmode(GPIO.BOARD)
GPIO.setup(button1, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(led1, GPIO.OUT)
GPIO.setup(button2, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(led2, GPIO.OUT)
GPIO.setup(button3, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(led3, GPIO.OUT)
GPIO.setup(button4, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(led4, GPIO.OUT)
GPIO.setup(button5, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(led5, GPIO.OUT)
GPIO.setup(button6, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(led6, GPIO.OUT)
GPIO.setup(button7, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(led7, GPIO.OUT)
GPIO.setup(button8, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(led8, GPIO.OUT)
GPIO.setup(button9, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(led9, GPIO.OUT)
#my functions to play videos
def playVid1(ev=None):
print('Button 1 Pressed...')
player = True
while player == True:
omxc = Popen(['omxplayer', '-b', movie1])
GPIO.output(led1, True)
time.sleep(15.0)
print('Button 1 after...')
GPIO.output(led1, False)
player = False
def playVid2(ev=None):
print("video2 is playing")
def playVid3(ev=None):
print("video3 is playing")
def playVid4(ev=None):
print("video4 is playing")
def playVid5(ev=None):
print("video5 is playing")
def playVid6(ev=None):
print("video6 is playing")
def playVid7(ev=None):
print("video7 is playing")
def playVid8(ev=None):
print("video8 is playing")
def playVid9(ev=None):
print("video9 is playing")
#the "mainloop"
def loop():
GPIO.output(led1, False) #sets led off
GPIO.output(led2, False)
GPIO.output(led3, False)
GPIO.output(led4, False)
GPIO.output(led5, False)
GPIO.output(led6, False)
GPIO.output(led7, False)
GPIO.output(led8, False)
GPIO.output(led9, False)
GPIO.add_event_detect(button1, GPIO.FALLING, callback=playVid1, bouncetime=200)
print("it still loops")
GPIO.add_event_detect(button2, GPIO.FALLING, callback=playVid2, bouncetime=200)
GPIO.add_event_detect(button3, GPIO.FALLING, callback=playVid3, bouncetime=200)
GPIO.add_event_detect(button4, GPIO.FALLING, callback=playVid4, bouncetime=200)
GPIO.add_event_detect(button5, GPIO.FALLING, callback=playVid5, bouncetime=200)
GPIO.add_event_detect(button6, GPIO.FALLING, callback=playVid6, bouncetime=200)
GPIO.add_event_detect(button7, GPIO.FALLING, callback=playVid7, bouncetime=200)
GPIO.add_event_detect(button8, GPIO.FALLING, callback=playVid8, bouncetime=200)
GPIO.add_event_detect(button9, GPIO.FALLING, callback=playVid9, bouncetime=200)
def endprogram():
GPIO.output(led1, GPIO.HIGH) #led off
GPIO.cleanup()
if __name__ == '__main__':
setup()
try:
loop()
except KeyboardInterrupt:
print( 'keyboard interrupt detected' )
endprogram()