视频未在按下按钮时停止,以及在视频播放期间LED未打开

2024-04-20 15:06:17 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我的目标是:

按下按钮,LED亮起并播放视频

视频播放完后,LED和视频必须自动关闭。此外,在播放视频时,可以手动按下按钮关闭LED和视频

此时,当我设置10秒延迟时,LED将亮起10秒。否则,LED将根本不亮起。此外,有时它会提示“omxplayer.bin:找不到进程”。有时,当我取消睡眠时,视频将无法播放

GPIO.setmode(GPIO.BCM)

button1=17 #pin for button
LED1=18 #pin for LED

GPIO.setup(button1, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(LED1, GPIO.OUT)

BS1=False #button state
last_state1 = True
input_state1 = True
player = False #video player state

while(1):


        #Read states of input
        input_state1 = GPIO.input(button1)

        #If GPIO(17) is shorted to Ground/ When the button is pressed.
        if input_state1 != last_state1:

            if GPIO.input(button1)==0:
                print ("LED ON")
                if BS1 ==False:
                    GPIO.output(LED1,True) #turn on LED
                    BS1=True

                elif (player and not input_state1):
                    print(player)
                    print(input_state1)

                    os.system('killall omxplayer.bin')
                    omxc = Popen(['omxplayer', '-b', movie1])
                    print ("Killall omxplayer")
                    player = True  

                elif not input_state1:
                    omxc = Popen(['omxplayer', '-b', movie1])
                    print ("Video Plays")
                    player = True
                    sleep(10)  


            else:
                    os.system('killall omxplayer.bin')
                    player = False
                    print ("Video Stops")

                    GPIO.output(LED1,False)
                    BS1=False
                    print ("LED OFF")
                    sleep(.5)
        #Set last_input states
        last_state1 = input_state1

GPIO.cleanup()

Tags: falsetrueinput视频ledgpiobinlast