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?