如何通过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.
我只学习python 3天.非常感谢您可以为我提供详细的代码.
I just learn python for 3days. I'm very appreciate that you can help me with detail code.
Popen.Terminate()或Kill()可以杀死临时文件,为什么不能杀死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屋!