How can I kill omxplayer player on a Raspberry Pi using Python
<p>I'm doing a DIY project using a Raspberry Pi 3 where I need to play 4 videos using omxplayer. </p>

<p>Each video is played once you press a certain button on the protoboard:</p>

<ul>
<li>Press Button 1 - Play Video 1</li>
<li>Press Button 2 - Play Video 2</li>
<li>Press Button 3 - Play Video 3</li>
<li>Press Button 4 - Play Video 4</li>
</ul>

<p>I was successful playing the 4 videos whenever I press any of the buttons using the following python code:</p>

<pre><code>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
</code></pre>

<p>Here is my issue. </p>

<p>When I press a button e.g Button 1, the whole video 1 starts playing properly on the screen. If I press any button while the video1 is running, nothing happens. What I want to achieve is that whenever I press any button on the protoboard, omxplayer should stop reproducing any video (if the is any playing) and start a new one.</p>

<p>I have read something about kill omxplayer using PIPE like they say in the following link but without success:</p>

<p><a href="http://stackoverflow.com/questions/20871658/how-can-i-kill-omxplayer-by-python-subprocess">How can I kill omxplayer by Python Subprocess</a></p>

<p>Any help will be appreciated</p>

<p>A bit hacky i guess but have you tried to killall before running the omxplayer?</p>

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

python raspberry-pi omxplayer