我对 Python 和 Raspberry Pi 很陌生,所以如果问题很幼稚,请原谅。我正在编写一个程序,它在由 PIR 传感器触发时播放视频。感应到的触发器是可见的,但是当我终止/停止 Thonny 中的程序时,视频会播放。
import os
import sys
from subprocess import Popen
import RPi.GPIO as GPIO
import time
movie1 = ("/home/pi/Desktop/2.mp4")
movie2 = ("/home/pi/Desktop/1.mp4")
pir_sensor = 11
GPIO.setmode(GPIO.BOARD)
GPIO.setup(pir_sensor, GPIO.IN)
current_state = 0
try:
while True:
time.sleep(0.1)
current_state = GPIO.input(pir_sensor)
if current_state == 1:
#print("GPIO pin %s is %s" % (pir_sensor, current_state))
os.system('killall omxplayer.bin')
omxc = Popen(['omxplayer', '-b', movie1])
except KeyboardInterrupt:
pass
finally:
GPIO.cleanup()
视频应在触发时立即播放。