0

使用 OMX 的 Raspberry Pi 自动播放视频,但有时会挂在 Time.Sleep()
这是使用 python 创建的代码,我使用时间模块在执行时播放视频或通过按下输入按钮进行更改,但没有 time.sleep() 它不会给出任何回应。如果我使用时间模块,那么它会播放视频。一段时间后,代码冻结,我不得​​不再次重新启动它。我非常急需解决方案

import os
import time
import sys
import subprocess
global streamVideo
before = False
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(18,GPIO.OUT)
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_UP)
while True:
    GPIO.output(18,GPIO.HIGH)
    input1 = GPIO.input(23)
    input2 = GPIO.input(24)
    try:
        if(input1==False and input2==False):
            # os.system('killall omxplayer.bin')
            streamVideo = subprocess.Popen(['omxplayer', '-o', 'hdmi', 'intro.mp4'],stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE, close_fds=True)
            time.sleep(0.5)
            # before=True
        if(input1==True):
            os.system('killall omxplayer.bin')
            while(input1):
                if GPIO.input(23)==False:
                    # input1=False
                    # input2=False
                    os.system('killall omxplayer.bin')
                    break
                streamVideo = subprocess.Popen(['omxplayer', '-o', 'hdmi', '15.mp4'],stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE, close_fds=True)
                time.sleep(3.5)
            # before=False
            continue
        if(input2==True):
            os.system('killall omxplayer.bin')
            while(input2):
                if GPIO.input(24)==False:
                    os.system('killall omxplayer.bin')
                    break
                streamVideo = subprocess.Popen(['omxplayer', '-o', 'hdmi', '15.mp4'],stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE, close_fds=True)
                time.sleep(3.5)
            continue
            # before=False
    except:
        print("Error: unable to start thread")
GPIO.cleanup()

编辑

import RPi.GPIO as GPIO
import os
import time
import sys
import subprocess
from imp import reload
global streamVideo
import threading
os.system("sudo fbi -T 2 -d /dev/fb0 -noverbose -a 13.jpg")
before = False
class TimerClass(threading.Thread):
        def __init__(self):
            threading.Thread.__init__(self)
            self.event = threading.Event()

        def run(self):
            input1 = GPIO.input(23)
            input2 = GPIO.input(24)
            if(input1==False and input2==False and not self.event.is_set()):
                # os.system('killall omxplayer.bin')
                streamVideo = subprocess.Popen(['omxplayer', '-o', 'hdmi', 'intro.mp4'],stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE, close_fds=True)
                # time.sleep(0.5)
                before=True
                self.event.wait(1)

        def stop(self):
            self.event.set()

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(18,GPIO.OUT)
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_UP)
while True:

    GPIO.output(18,GPIO.HIGH)
    input1 = GPIO.input(23)
    input2 = GPIO.input(24)

    # try:
    tmr = TimerClass()
    tmr.start()

    time.sleep(0.5)

    if(input1==True):
        os.system('killall omxplayer.bin')
        while(input1):
            if GPIO.input(23)==False:
                tmr.stop()
                os.system('killall omxplayer.bin')
                break
            streamVideo = subprocess.Popen(['omxplayer', '-o', 'hdmi', '15.mp4'],stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE, close_fds=True)
            time.sleep(3.5)
    if(input2==True):
        os.system('killall omxplayer.bin')
        while(input2):
            if GPIO.input(24)==False:
                tmr.stop()
                os.system('killall omxplayer.bin')
                break
            streamVideo = subprocess.Popen(['omxplayer', '-o', 'hdmi', '15.mp4'],stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE, close_fds=True)
            time.sleep(3.5)
    tmr.stop()
    reload(time)

    # except:
    #     print("Error: unable to start thread")
GPIO.cleanup()

4

0 回答 0