如何用Python Subprocess终止omxplayer进程

4
我在玩我的树莓派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()
2个回答

5

您可以通过管道发送命令,以下是一个示例:

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')

有时候这个不起作用,那么你就需要进行一个 flush 操作。

playProcess.stdin.flush()

0
谢谢您关于如何使用Python关闭omxplayer的提示等。
使用以下代码,我得到了列出的错误。
  streamVideo = subprocess.Popen(['omxplayer', '-o', 'local', 'Media/TestA.mp4'],   stdin=subprocess.PIPE, stdout=subprocess.PIPE,      stderr=subprocess.PIPE, close_fds=True)
  time.sleep(10)
  streamVideo.stdin.write('q')
  streamVideo.stdin.flush()

错误: streamVideo.stdin.write('q') IOError: [Errno 32] 管道破裂

如何在Python中终止omxplayer

streamVideo = Popen(['omxplayer', '-o', 'local', 'Media/TestA.mp4'])
time.sleep(10)  # Time for the clip to play
streamVideo = Popen(['omxplayer', '-i', 'Media/TestA.mp4'])  # Kills the Display

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接