如何使用Python杀死Raspberry Pi上的omxplayer播放器
问题描述:
我正在使用Raspberry Pi 3做一个DIY项目,我需要使用omxplayer播放4个视频。如何使用Python杀死Raspberry Pi上的omxplayer播放器
一旦你按下面包板某个按钮每个视频播放:
- 按下按钮1 - 播放视频1个
- 按下按钮2 - 播放视频2
- 按钮3 - 播放视频3
- 按按钮4 - 播放视频4
我成功打4个视频时,我按下任何按钮使用以下python代码:
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运行时按下任何按钮,则什么也不会发生。我想要实现的是,只要按下原型板上的任何按钮,omplayer就应该停止复制任何视频(如果有任何播放)并开始新视频。
我读过一些关于杀omxplayer使用PIPE就像他们在下面的链接,但没有成功说:
How can I kill omxplayer by Python Subprocess
任何帮助将不胜感激
答
有点哈克我猜,但有你在运行omxplayer之前尝试过killall?
command = "killall omxplayer; omxplayer -p -o hdmi %s/%s.mp4" % (pathVideos,nameVideo)
os.system(command)
答
我改性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
我还添加&在命令2到底为了让命令在后台运行
有点“哈克”,但为我工作:)
答
我的解决方案是给视频一个会话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)
谢谢Matz!尽管上面的代码没有按预期工作。它把我带到另一个正常工作的解决方案。 – Iker