如何通过 Python 子进程杀死 omxplayer [英] How can I kill omxplayer by Python Subprocess
问题描述
我正在玩我的 Raspberry Pi GPIO.我将 4 个开关连接到 GPIO.
I'm playing with my Raspberry Pi GPIO. I connect 4 switch to GPIO.
我要实现的功能是
按住开关 1. 停止当前电影,播放 M01.mp4.
While hold switch 1. Stop the current movie, Play M01.mp4.
按住开关2的同时停止当前电影,播放M02.mp4.
While hold switch 2. Stop the current movie, Play M02.mp4.
...
如果没有按住开关,播放器 M00.mp4 循环播放.
If no switch was holded, player M00.mp4 in loop.
我只学了 3 天 Python.非常感谢您能帮我提供详细代码.
I just learn python for 3days. I'm very appreciate that you can help me with detail code.
Popen.Terminate() or Kill() 可以杀scratch,为什么不能杀omxplayer?
Popen.Terminate() or Kill() can kill scratch, why can not kill 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()
推荐答案
你能做的最好的事情是通过管道发送命令,见下面的例子
The best thing you could do is to send the command though pipe see the below example
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')
有时这行不通,那么您必须进行冲洗
some times this won't work then you have to do a flush
playProcess.stdin.flush()
这篇关于如何通过 Python 子进程杀死 omxplayer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!