【问题标题】:How to pause a subprocess (Python 2.2.6)?如何暂停子进程(Python 2.2.6)?
【发布时间】: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


    【解决方案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.
    

    【讨论】:

    • 谢谢!它就像一个魅力。我欠你很多时间:)
    • 我不知道有任何其他方法对我这个新手来说足够简单,可以理解。 :/ 到目前为止,它对我来说效果很好。
    • @jmcclaire 如果它对你有用,那么请坚持下去。不过,您可以做几件事来改进设计。如果您愿意,您可以将代码发布到codereview.stackexchange.com,我相信人们会为您审核。
    • @J.F.Sebastian 我尝试了您发送给我的代码,但由于我是新手,无法让它工作。我一开始就告诉过你。你不必对它刻薄。 “失去了耐心”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-10
    • 2022-11-18
    • 2017-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多