python - 如何通过 Python 子进程杀死 omxplayer

标签 python subprocess raspberry-pi gpio

我正在玩我的 Raspberry Pi GPIO。我将 4 个开关连接到 GPIO。

我要实现的功能是

按住开关 1。停止当前电影,播放 M01.mp4。

按住开关 2。停止当前电影,播放 M02.mp4。

...

如果没有按住开关,则循环播放 M00.mp4。

我刚学了 3 天的 python。非常感谢您能帮助我提供详细代码。

Popen.Terminate()或Kill()可以杀scratch,为什么不能杀omxplayer?

#!/usr/bin/env python2.7
import subprocess,time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(25, GPIO.IN)
GPIO.setup(24, GPIO.IN)
GPIO.setup(23, GPIO.IN)
GPIO.setup(22, GPIO.IN)
while True:
    if GPIO.input(25) == True:
        time.sleep(1)
        playProcess=subprocess.Popen(['scratch'],stdout=True)
        #Terminate() or Kill() can kill scratch.
        playProcess=subprocess.Popen(['omxplayer','/home/pi/pyStudy/DSCF4021.MP4'],stdout=True)
        #Terminate() or Kill() CAN NOT kill scratch.
        time.sleep(5)
        playProcess.terminate()

最佳答案

你能做的最好的事情就是通过管道发送命令,见下面的例子

global playProcess
playProcess=subprocess.Popen(['omxplayer','/home/pi/pyStudy/DSCF4021.MP4'],stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE, close_fds=True)
time.sleep(10)
playProcess.stdin.write('q')

有时这是行不通的,所以你必须冲水

playProcess.stdin.flush()

关于python - 如何通过 Python 子进程杀死 omxplayer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20871658/

相关文章:

python - 将数据传递给向导 odoo 11

python - `No numeric types to aggregate` 滚动总和和 timedelta 类型错误

python - 操作系统如何处理 python 和 python 脚本的子进程......?

zend-framework - Raspberry Pi 上的 Zend Framework 抛出 Zend_Config_Exception 'Section ' ' 无法在 (...) 中找到'

python - 对 Zoho Creator 返回的数据使用 json.loads() - extra_data() 错误

python - 使用命令行时出现 App Engine 问题

ruby - 从 Ruby 的反引号捕获 "command not found"错误?

linux - 使用子进程在 Python 中运行 linux 命令时出错

java - Raspberry Pi 上 Java 3D 中的段错误

c# - .NETCore 应用程序因 StackOverflowException 而终止