2014-10-27 58 views
0

我在用Python创建的视频循环中遇到了一些麻烦。我想要做的是播放视频循环,然后当按下按钮(RPi GPIO)时,它将播放不同的视频。一旦该视频播放完毕,它应该返回播放循环播放的视频。除了在其他正在播放的视频播放之前,循环视频将开始播放之前,以下代码一切都很好。我不确定是否这是一个问题,我如何做循环或如果我需要暂停子进程。如何暂停一个子进程(Python 2.2.6)?

非常感谢你们提供的任何帮助和建议!

#!/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() 
+0

哦蟒蛇2.2.6 ... – Anzel 2014-10-27 15:49:43

回答

-1

welcomeLoop功能将开始运行的欢迎视频每25秒,不管是怎么回事,与其他影片。您需要设置时的其他影片一个开始播放一个标志,让welcomeLoop功能将不会启动重放欢迎视频在他们的顶部:

video_playing = False 

def welcome_loop(): 
    global playProcess 
    while True: 
     x = 1 
     if not video_playing: 
      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(): 
    global video_playing 
    while True: 
     if GPIO.input(9): 
      video_playing = True # Set the flag 
      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') 
      video_playing = False # unset the flag 
      time.sleep(.5) 
      #welcome_loop() 

    # Same pattern for the other videos. 
+0

谢谢!它像一个魅力。我欠你很多时间:) – jmcclaire 2014-10-27 16:01:24

+0

我不知道有任何其他的方式来做到这一点,这对我来说很简单,一个新手,了解。 :到目前为止,它对我来说工作很好。 – jmcclaire 2014-10-29 16:25:59

+0

@jmcclaire如果它适合你,那么随时可以坚持下去。不过,有几件事可以改善设计。如果你愿意,你可以在http://codereview.stackexchange.com上发布代码,我相信人们会为你检查它。 – dano 2014-10-29 16:29:15