繁体   English   中英

如何暂停子进程(Python 2.2.6)?

[英]How to pause a subprocess (Python 2.2.6)?

我在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()

无论其他视频发生什么情况,您的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.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM