I am new to Python.

What I want to do is to play different videos depends on distance using an Ultrasonic distance sensor.

For example,

if the distance <= 20, play movie1.

if the distance > 20, play movie2.

It works, and that seems quite simple.

But I have a problem. The video is not changed instantly. After playing it completely, it responds again.

So even though the distance is <20, the movie1 is still going. After finishing it, movie2 appears.

I want to change the video as soon as the distance is changed, not waiting until the video is finished. Maybe I need some code to kill OMXPlayer? I have no idea.

Below is my code.

import RPi.GPIO as GPIO
import time, sys, os
from subprocess import Popen
import subprocess as sp

distance = 400
zone = 1  
# to know if omxplayer is playing (= None) or not

#set GPIO Pins
#set GPIO direction (IN / OUT)

# Videos definitions
movie1 = ('/home/pi/Videos/test1.mp4')
movie2 = ('/home/pi/Videos/test2.mp4')

def distance():
    # set Trigger to HIGH
    GPIO.output(GPIO_TRIGGER, True)
    # set Trigger after 0.01ms to LOW
    GPIO.output(GPIO_TRIGGER, False)
    StartTime = time.time()
    StopTime = time.time()
    # save StartTime
    while GPIO.input(GPIO_ECHO) == 0:
        StartTime = time.time()
    # save time of arrival
    while GPIO.input(GPIO_ECHO) == 1:
        StopTime = time.time()
    # time difference between start and arrival
    TimeElapsed = StopTime - StartTime
    # multiply with the sonic speed (34300 cm/s)
    # and divide by 2, because there and back
    distance = (TimeElapsed * 34300) / 2

    return distance

if __name__ == '__main__':
        while True:
            dist = distance()
            print ("Measured Distance = %.1f cm" % dist)
            if (distance() <= 20):
                zone = 1
            elif (distance() > 20):
                zone = 2
            if (zone == 1):
                Popen(['omxplayer', '-b', movie1])
            if (zone == 2):
                Popen(['omxplayer', '-b', movie2])
    except KeyboardInterrupt:
    # exits when you press CTRL+C
      os.system('killall omxplayer.bin')

2 Answers 2


There is no need for blood ;) - when the distance changes, before

Popen(['omxplayer', '-b', movie2])

issue a command to stop the currently playing video

  • What is that command to stop a playing video? Commented Dec 2, 2021 at 7:22
  • Please explain what the command does. Commented Dec 28, 2022 at 2:07

Type the command,

$ ps -A | grep omxplayer 

Into a terminal session while the video is running. It should give you the number of the process that you can kill.

Using the command...

$ kill [processnumber]

Then you can use those same shell commands as sub processes in your Python code once you get it to work on a terminal session.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.