【发布时间】:2014-12-22 20:07:50
【问题描述】:
我在使用 Python 创建的视频循环时遇到了一些问题。我想要做的是播放视频循环,然后当按下按钮(RPi GPIO)时,它将播放不同的视频。一旦该视频播放完毕,它应该返回播放循环视频。下面的代码一切正常,除了循环视频将在其他正在播放的视频之前开始播放。我不确定这是否是我如何执行循环的问题,或者我是否需要暂停子进程。
非常感谢您提供的任何帮助和建议!
#!/usr/bin/python
from time import sleep
import RPi.GPIO as GPIO
import subprocess
import time
import thread
GPIO.setmode (GPIO.BCM)
GPIO.setwarnings (False)
GPIO.setup(9, GPIO.IN)
GPIO.setup(10, GPIO.IN)
GPIO.setup(11, GPIO.IN)
GPIO.setup(17, GPIO.OUT)
GPIO.setup(22, GPIO.OUT)
GPIO.setup(27, GPIO.OUT)
def welcome_loop():
while True:
global playProcess
x = 1
print "Play Welcome Video"
time.sleep(.5)
playProcess=subprocess.Popen(['omxplayer','-b','Desktop/videos/loop/loop.mp4'],stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,close_fds=True)
time.sleep(25)
x += 1
def videos():
while True:
if GPIO.input(9):
print "Stop Welcome Video"
time.sleep(.5)
playProcess.stdin.write('q')
time.sleep(.5)
print "Play Sippycup Video"
sippycup_video=subprocess.Popen(['omxplayer','-b','Desktop/videos/sippycup.mp4'],stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,close_fds=True)
time.sleep(30)
sippycup_video.stdin.write('q')
time.sleep(.5)
#welcome_loop()
if GPIO.input(10):
print "Stop Welcome Video"
time.sleep(.5)
playProcess.stdin.write('q')
time.sleep(.5)
print "Play Shoppingcart Video"
shoppingcart_video=subprocess.Popen(['omxplayer','-b','Desktop/videos/shoppingcart.mp4'],stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,close_fds=True)
time.sleep(30)
shoppingcart_video.stdin.write('q')
time.sleep(.5)
#welcome_loop()
if GPIO.input(11):
print "Stop Welcome Video"
time.sleep(.5)
playProcess.stdin.write('q')
time.sleep(.5)
print "Play Dodgeballs Video"
Dodgeballs_video=subprocess.Popen(['omxplayer','-b','Desktop/videos/dodgeballs.mp4'],stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,close_fds=True)
time.sleep(30)
Dodgeballs_video.stdin.write('q')
time.sleep(.5)
#welcome_loop()
thread.start_new_thread( videos, () )
thread.start_new_thread( welcome_loop, () )
while True:
pass
GPIO.cleanup()
【问题讨论】:
标签: python multithreading video subprocess gpio