2014-10-27 59 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