Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在播放视频时打开和关闭omxplayer(Python/Raspberry Pi)?_Python_Multithreading_Subprocess_Raspberry Pi_Gpio - Fatal编程技术网

如何在播放视频时打开和关闭omxplayer(Python/Raspberry Pi)?

如何在播放视频时打开和关闭omxplayer(Python/Raspberry Pi)?,python,multithreading,subprocess,raspberry-pi,gpio,Python,Multithreading,Subprocess,Raspberry Pi,Gpio,使用树莓圆周率和一些按钮,我想控制视频播放。当有人按下按钮时,相应的视频播放。按钮很好用。当您按下一个按钮时,视频将播放,但当您按下另一个按钮或同一个按钮时,视频将打开,而不会关闭当前正在播放的视频。我已经找了一段时间来解决这个问题。我是Python新手,所以请尽量简单。在下面的代码中,我尝试使用多线程来完成它。但是,当另一个线程启动时,我无法关闭该线程。我可以在视频播放10秒后关闭它,但是我不能将quit命令移动到其他任何地方来关闭其他视频:playSippycup.stdin.write('

使用树莓圆周率和一些按钮,我想控制视频播放。当有人按下按钮时,相应的视频播放。按钮很好用。当您按下一个按钮时,视频将播放,但当您按下另一个按钮或同一个按钮时,视频将打开,而不会关闭当前正在播放的视频。我已经找了一段时间来解决这个问题。我是Python新手,所以请尽量简单。在下面的代码中,我尝试使用多线程来完成它。但是,当另一个线程启动时,我无法关闭该线程。我可以在视频播放10秒后关闭它,但是我不能将quit命令移动到其他任何地方来关闭其他视频:playSippycup.stdin.write('q')

以下是我目前收到的错误:

Unhandled exception in thread started by <function shoppingcart at 0xb6c566f0>Playing Sippycup

Unhandled exception in thread started by <function dodgeballs at 0xb6c56670>
Traceback (most recent call last):
    File "./labmural2.py", line 53, in dodgeballs
    playDodgeballs.stdin.write('q')
NameError: global name 'playDodgeballs' is not defined
Traceback (most recent call last):
  File "./labmural2.py", line 71, in shoppingcart
    playShoppingcart.stdin.write('q')
NameError: global name 'playShoppingcart' is not defined

新编辑:

#!/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 "LOOPING"
            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(10)
            playProcess.stdin.write('q')
            x += 1

def videos():
    while True:
            if GPIO.input(9):
                    print "STOP LOOP"
                    time.sleep(.5)
                    playProcess.stdin.write('q')
                    time.sleep(.5)
                    print "Play Sippycup"
                    sippycup_video=subprocess.Popen(['omxplayer','-b','Desktop/videos/sippycup.mp4'],stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE, close_fds=True)
                    time.sleep(10)
                    sippycup_video.stdin.write('q')
                    time.sleep(.5)
                    welcome_loop()

            if GPIO.input(10):
                    print "STOP LOOP"
                    time.sleep(.5)
                    playProcess.stdin.write('q')
                    time.sleep(.5)
                    print "Play Dodgeballs"
                    dodgeballs_video=subprocess.Popen(['omxplayer','-b','Desktop/videos/dodgeballs.mp4'],stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE, close_fds=True)
                    time.sleep(10)
                    dodgeballs_video.stdin.write('q')
                    time.sleep(.5)
                    welcome_loop()

            if GPIO.input(11):
                    print "STOP LOOP"
                    time.sleep(.5)
                    playProcess.stdin.write('q')
                    time.sleep(.5)
                    print "Play Shoppingcart"
                    shoppingcart_video=subprocess.Popen(['omxplayer','-b','Desktop/videos/shoppingcart.mp4'],stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE, close_fds=True)
                    time.sleep(10)
                    shoppingcart_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()
错误:

#!/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 "LOOPING"
            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(10)
            playProcess.stdin.write('q')
            x += 1

def videos():
    while True:
            if GPIO.input(9):
                    print "STOP LOOP"
                    time.sleep(.5)
                    playProcess.stdin.write('q')
                    time.sleep(.5)
                    print "Play Sippycup"
                    sippycup_video=subprocess.Popen(['omxplayer','-b','Desktop/videos/sippycup.mp4'],stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE, close_fds=True)
                    time.sleep(10)
                    sippycup_video.stdin.write('q')
                    time.sleep(.5)
                    welcome_loop()

            if GPIO.input(10):
                    print "STOP LOOP"
                    time.sleep(.5)
                    playProcess.stdin.write('q')
                    time.sleep(.5)
                    print "Play Dodgeballs"
                    dodgeballs_video=subprocess.Popen(['omxplayer','-b','Desktop/videos/dodgeballs.mp4'],stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE, close_fds=True)
                    time.sleep(10)
                    dodgeballs_video.stdin.write('q')
                    time.sleep(.5)
                    welcome_loop()

            if GPIO.input(11):
                    print "STOP LOOP"
                    time.sleep(.5)
                    playProcess.stdin.write('q')
                    time.sleep(.5)
                    print "Play Shoppingcart"
                    shoppingcart_video=subprocess.Popen(['omxplayer','-b','Desktop/videos/shoppingcart.mp4'],stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE, close_fds=True)
                    time.sleep(10)
                    shoppingcart_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()
由启动的线程中存在未处理的异常 回溯(最近一次呼叫最后一次): 文件“/labmural2.py”,第28行,在欢迎循环中 playProcess.stdin.write('q')
IOError:[Errno 32]管道破裂

名称错误:未定义全局名称“Playdowgeballs”
表示您试图在定义前使用
Playdowgeballs

我将通过删除所有全局变量和线程来简化代码<代码>子流程。Popen运行单独的流程;它不会阻止您的主线程:

names = 'sippycup', 'dodgeballs', 'shoppingcart'
movies = ['Desktop/videos/{name}.mp4'.format(name=name) for name in names]
players = [Player(movie=movie) for movie in movies]
player = players[0]

setup_io() # GPIO setup
while True:
    for key in get_key_events(): # get GPIO input
        if key == '0':
           player = players[0]
        elif key == 'space':
           player.toggle() # pause/unpause
        elif key == 'enter':
           player.start()
        ...
其中
Player
是围绕
omxplayer
子流程的简单包装:

import logging
from subprocess import Popen, PIPE, DEVNULL

logger = logging.getLogger(__name__)

class Player:
    def __init__(self, movie):
       self.movie = movie
       self.process = None

    def start(self):
        self.stop()
        self.process = Popen(['omxplayer', self.movie], stdin=PIPE,
                             stdout=DEVNULL, close_fds=True, bufsize=0)
        self.process.stdin.write(start_command) # start playing

    def stop(self):
        p = self.process
        if p is not None:
           try:
               p.stdin.write(quit_command) # send quit command
               p.terminate()
               p.wait() # -> move into background thread if necessary
           except EnvironmentError as e:
               logger.error("can't stop %s: %s", self.movie, e)
           else:
               self.process = None

    def toggle(self):
        p = self.process
        if p is not None:
           try:
               p.stdin.write(toggle_command) # pause/unpause
           except EnvironmentError as e:
               logger.warning("can't toggle %s: %s", self.movie, e)

指定适当的
启动\u命令
退出\u命令
切换\u命令
。根据omxplayer理解的命令和需要的命令,您可以定义不同的方法。

如果x==True:,请不要使用
,如果x:
,请使用
。我知道发生了什么,但不知道如何用我的代码实现它。谢谢你的帮助。@jmcclaire:如果它是压倒性的;不要试图一次完成所有任务。一次实现、测试一个功能/方法。例如,
setup\u io()
就是您问题中
def sippycup()
之前的代码。您是否了解如何实现
get_key_events()
:获取输入并将其解释为
space
enter
键被释放(如果需要,您可以将其展开以检测何时按下一个键、同时按下多个键、
Ctrl
等)。关于
Player.stop
方法有什么不清楚的地方?
omxplayer
是否接受
quit\u命令
命令,例如
q
或其他什么?不,我从未执行过get\u key\u事件()。我对Python很陌生。我的大部分经验都是与Arduino合作的。需要定义setup_io()。我如何定义它?omxplayer接受“q”作为退出。我可以按上面我的原始代码的格式退出视频,但不能在其他地方使用该命令,因为它说它尚未定义/如何导入DEVNULL?在Python2上,您可能需要模拟它。顺便说一句,您可以在google中搜索
subprocess devnull
,在第一个链接中获得类似的代码。