问题描述
使用 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,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','15.mp4'],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',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',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,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',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',close_fds=True)
time.sleep(3.5)
tmr.stop()
reload(time)
# except:
# print("Error: unable to start thread")
GPIO.cleanup()
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)