0

I'm trying to interrupt and stop a video with a switch interrupt and then play an alternative video. Here is what I tried so far.

import subprocess
import signal
import sys
import RPi.GPIO as GPIO
import time
from time import sleep

BUTTON1_GPIO = 2
BUTTON2_GPIO = 3
 
Led1 = 4
Led2 = 14
flag_0 = 0 
button = 0

def signal_handler(sig, frame):
    GPIO.cleanup()
    sys.exit(0)

def Picture_0():
    filename = "welcome.jpeg"
    import webbrowser
    webbrowser.open(filename)

def video0():
    video = 'cvlc --play-and-exit  --no-video-deco Intro_Vid.mp4'
    subprocess.run(video, shell=True)    
       
def video1():
    video = 'cvlc --play-and-exit  --no-video-deco 1.mp4' 
    subprocess.run(video, shell=True)
   
def video2():
    video = 'cvlc --play-and-exit  --no-video-deco 2.mp4'
    subprocess.run(video, shell=True)

GPIO.setmode(GPIO.BCM)
GPIO.setup(BUTTON1_GPIO, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(BUTTON2_GPIO, GPIO.IN, pull_up_down=GPIO.PUD_UP)

def interrupt_handler(channel):
    global button
    print("interrupt handler")

    if channel == BUTTON1_GPIO:
        button = 1
    elif channel == BUTTON2_GPIO:
        button = 2

GPIO.add_event_detect(
    BUTTON1_GPIO, GPIO.FALLING,
    callback=interrupt_handler, bouncetime=500
)
GPIO.add_event_detect(
    BUTTON2_GPIO, GPIO.FALLING, 
    callback=interrupt_handler, bouncetime=500
)
GPIO.setup(Led1, GPIO.OUT)
GPIO.setup(Led2, GPIO.OUT)
GPIO.output(Led1, GPIO.HIGH)    # Led off
GPIO.output(Led1, GPIO.HIGH)

while True:
    if button == 0:
        button = 100
        print("Button 0")
        GPIO.output(Led1, GPIO.HIGH)
        GPIO.output(Led2, GPIO.HIGH)
        video0()
    elif button == 1:
        print("Button 1")
        GPIO.output(Led1, GPIO.LOW)    # Led on
        video1()
        button = 0
    elif button == 2:
        print("Button 2")
        GPIO.output(Led2, GPIO.LOW)
        video2()
        button = 0

My code does capture the interrupt but the first video plays in its entirety before the videoplayer proceeds to the next video selected by the switch, rather than being stopping the first video as I require.

What am I missing?

sanitizedUser
  • 1,723
  • 3
  • 18
  • 33
Richard
  • 1
  • 1
  • Where do you tell `cvlc` to stop the first video? I don't see it. Also you don't need to use `while [True]`, use `while True` without the list syntax. – sanitizedUser Jun 24 '21 at 05:31
  • I wish to stop Video(0) in the Interrupt handler, what instruction would I use? I know that the interrupt is being called because I can use it to set a led, the video however just keeps on running until the end. – Richard Jun 24 '21 at 14:05

1 Answers1

0

First, save the subprocess into a global variable:

def video0():
    global videoProcess

    video = 'cvlc --play-and-exit  --no-video-deco Intro_Vid.mp4'
    videoProcess = subprocess.run(video, shell=True)

Then kill it before starting the second video:

while True:
    if button == 0:
        ...
    elif button == 1:
        ...
        videoProcess.kill()
        video1()
        ...
    elif button == 2:
        ...

If that doesn't kill the subprocess, it's likely because its child is still hanging.

To resolve this you can try using shell=False or find a more complicated way of terminating the subprocess in this question.

sanitizedUser
  • 1,723
  • 3
  • 18
  • 33