如何通过Python子进程杀死omxplayer

2024-05-16 21:46:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我在玩我的覆盆子果酱。我将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()

Tags: inimporttruegpio电影timesetupmp4
2条回答

最好的方法是通过管道发送命令,参见下面的示例

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关闭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

相关问题 更多 >