如何使用Python杀死Raspberry Pi上的omxplayer播放器

时间:2016-09-15 16:10:50

标签: python raspberry-pi omxplayer

我正在使用Raspberry Pi 3进行DIY项目,我需要使用omxplayer播放4个视频。

按下原型板上的某个按钮后,每个视频都会播放:

  • 按下按钮1 - 播放视频1
  • 按下按钮2 - 播放视频2
  • 按下按钮3 - 播放视频3
  • 按下按钮4 - 播放视频4

每当我使用以下python代码按下任何按钮时,我就成功播放了4个视频:

import RPi.GPIO as GPIO
import time
import os

GPIO.setmode(GPIO.BCM)   # Declaramos que los pines seran llamados como numeros
GPIO.setwarnings(False)

GPIO.setup(4, GPIO.IN)  # GPIO  7 como entrada
GPIO.setup(17, GPIO.IN) # GPIO 17 como entrada
GPIO.setup(27, GPIO.IN) # GPIO 27 como entrada
GPIO.setup(22, GPIO.IN) # GPIO 22 como entrada

pathVideos = "/home/pi/VideoHD/Belen"                   # Directorio donde se encuentran los videos en HD

def reproducirVideos(nameVideo):
    command = "omxplayer -p -o hdmi %s/%s.mp4" % (pathVideos,nameVideo)
    os.system(command)
    print "Reproduciendo el Video: %s " % nameVideo

def programaPrincipal():
    print("Inicio")

    while True:
        if (GPIO.input(4)):
            print("Iniciando Video: AMANECER")
            reproducirVideos("amanecer")
        elif (GPIO.input(17)):
            print("Iniciando Video: DIA")
            reproducirVideos("dia")
        elif (GPIO.input(27)):
            print("Iniciando Video: ATARDECER")
            reproducirVideos("atardecer")
        elif (GPIO.input(22)):
            print("Iniciando Video: ANOCHECER")
            reproducirVideos("anochecer")
        else:
            pass
    print("Fin de programa")
    GPIO.cleanup() #Limpiar los GPIO  


programaPrincipal()                           #Llamamos a la funcion blinkLeds para ejecutar el programa

这是我的问题。

当按下按钮(例如按钮1)时,整个视频1开始在屏幕上正常播放。如果我在video1运行时按任何按钮,则没有任何反应。我想要实现的是,每当我按下protoboard上的任何按钮时,omxplayer应该停止再现任何视频(如果是任何播放)并开始一个新视频。

我已经阅读了有关使用PIPE杀死omxplayer的内容,就像他们在以下链接中说的那样但没有成功:

How can I kill omxplayer by Python Subprocess

任何帮助将不胜感激

3 个答案:

答案 0 :(得分:1)

我猜有点hacky但是你在运行omxplayer之前试过killall吗?

command = "killall omxplayer; omxplayer -p -o hdmi %s/%s.mp4" % (pathVideos,nameVideo)
os.system(command)

答案 1 :(得分:0)

我使用以下代码修改了 reproducirVideos()功能,以便杀死 omxplayer

的任何进程
def reproducirVideos(nameVideo):
    command1 = "sudo killall -s 9 omxplayer.bin"
    os.system(command1)
    command2 = "omxplayer -p -o hdmi %s/%s.mp4 &" % (pathVideos,nameVideo)
    os.system(command2)
    print "Reproduciendo el Video: %s " % nameVideo

我还在 command2 的末尾添加了& ,以便在后台运行命令

一点点" hacky"但对我来说工作得很好:))

答案 2 :(得分:0)

我的解决方案是为视频提供会话ID,以便您稍后可以通过ID终止该进程。这是一个简单的视频转发器:

import os, signal, subprocess
import pifacedigitalio as pfd
from time import sleep

# Import movie names from /home/pi/video in alphabetical order. Note that movie 0 will loop when another is not playing.
names = [f for f in os.listdir('/home/pi/video') if os.path.isfile(os.path.join('/home/pi/video', f))]
movies = ['/home/pi/video/{name}'.format(name=name) for name in names]
movies.sort()

pfd.init()
loopMovie = 0
sleep (10)

# Start first instance of movie 0. Note that all processes started get a session ID so they can all be killed together with killpg.
# Add '-o', 'local' to the omxplayer operators to get local audio (headphone jack) instead of HDMI
playProcess=subprocess.Popen(['omxplayer','-b',movies [loopMovie]], stdout=subprocess.PIPE, preexec_fn=os.setsid)

while True :
   # One piFace import board has 8 inputs numbered 0-7
   for b in range (8) :
      if pfd.digital_read(b) == 1 and b + 1 < len(movies) :
         if playProcess.poll() != 0 : os.killpg(os.getpgid(playProcess.pid), signal.SIGTERM)
         playProcess=subprocess.Popen(['omxplayer','-b', '-o', 'local', movies [b+1]], stdout=subprocess.PIPE, preexec_fn=os.setsid)
   # if nothing is playing, restart movie 0.
   if playProcess.poll() == 0 :
      playProcess=subprocess.Popen(['omxplayer','-b',movies [0]], stdout=subprocess.PIPE, preexec_fn=os.setsid)
   sleep (.1)