2016-09-15 198 views
0

我正在使用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

任何幫助將不勝感激

回答

1

有點哈克我猜,但有你在運行omxplayer之前嘗試過killall?

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

謝謝Matz!儘管上面的代碼沒有按預期工作。它把我帶到另一個正常工作的解決方案。 – Iker

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 

我還添加&命令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)