0

Hello im a newbie in python and try to play multiple videos on pushbuttons. (a designer that tries to make an installation with a raspberry 3 b). I have a "mainloop" that works with event detections and callback functions. That part works. In my function playVid1() i try to open the omxplayer with my video. The first time it works pretty fine, expect that it seems to not close the player correctly and doesn't return in the loop. When i click a second time on the button, again the player opens, but doesn't play my video anymore. I would be really happy if someone could help me out. Any hints? Thanks and best regards

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

#pins on raspy

button1 = 23
button2 = 21
button3 = 19
button4 = 15
button5 = 13
button6 = 11
button7 = 7
button8 = 29
button9 = 31

led1 = 36
led2 = 26
led3 = 24
led4 = 22
led5 = 18
led6 = 16
led7 = 12
led8 = 38
led9 = 40


movie1 = ("/home/pi/Videos/video1.mp4")
video1_length = 10.0 # seconds before led turns off and 
player = False

def setup():
    
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(button1, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    GPIO.setup(led1, GPIO.OUT)
    GPIO.setup(button2, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    GPIO.setup(led2, GPIO.OUT)
    GPIO.setup(button3, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    GPIO.setup(led3, GPIO.OUT)
    GPIO.setup(button4, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    GPIO.setup(led4, GPIO.OUT)
    GPIO.setup(button5, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    GPIO.setup(led5, GPIO.OUT)
    GPIO.setup(button6, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    GPIO.setup(led6, GPIO.OUT)
    GPIO.setup(button7, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    GPIO.setup(led7, GPIO.OUT)
    GPIO.setup(button8, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    GPIO.setup(led8, GPIO.OUT)
    GPIO.setup(button9, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    GPIO.setup(led9, GPIO.OUT)

#my functions to play videos

def playVid1(ev=None):
    print('Button 1 Pressed...')
    player = True
    while player == True:
        omxc = Popen(['omxplayer', '-b', movie1])
        GPIO.output(led1, True)
        time.sleep(15.0)
        print('Button 1 after...')
        GPIO.output(led1, False)
        player = False
        
def playVid2(ev=None):
    print("video2 is playing")

def playVid3(ev=None):
    print("video3 is playing")

def playVid4(ev=None):
    print("video4 is playing")

def playVid5(ev=None):
    print("video5 is playing")

def playVid6(ev=None):
    print("video6 is playing")

def playVid7(ev=None):
    print("video7 is playing")

def playVid8(ev=None):
    print("video8 is playing")

def playVid9(ev=None):
    print("video9 is playing")

#the "mainloop"

def loop():

    GPIO.output(led1, False) #sets led off
    GPIO.output(led2, False)
    GPIO.output(led3, False)
    GPIO.output(led4, False)
    GPIO.output(led5, False)
    GPIO.output(led6, False)
    GPIO.output(led7, False)
    GPIO.output(led8, False)
    GPIO.output(led9, False)



    GPIO.add_event_detect(button1, GPIO.FALLING, callback=playVid1, bouncetime=200)
    print("it still loops")
    GPIO.add_event_detect(button2, GPIO.FALLING, callback=playVid2, bouncetime=200)
    GPIO.add_event_detect(button3, GPIO.FALLING, callback=playVid3, bouncetime=200)
    GPIO.add_event_detect(button4, GPIO.FALLING, callback=playVid4, bouncetime=200)
    GPIO.add_event_detect(button5, GPIO.FALLING, callback=playVid5, bouncetime=200)
    GPIO.add_event_detect(button6, GPIO.FALLING, callback=playVid6, bouncetime=200)
    GPIO.add_event_detect(button7, GPIO.FALLING, callback=playVid7, bouncetime=200)
    GPIO.add_event_detect(button8, GPIO.FALLING, callback=playVid8, bouncetime=200)
    GPIO.add_event_detect(button9, GPIO.FALLING, callback=playVid9, bouncetime=200)
   

def endprogram():
    GPIO.output(led1, GPIO.HIGH) #led off
    GPIO.cleanup()


if __name__ == '__main__':
      
      setup()
      
      try:
             loop()
      
      except KeyboardInterrupt:
             print( 'keyboard interrupt detected' )
             endprogram()
furas
  • 134,197
  • 12
  • 106
  • 148
  • instead of separated variables - `button1`, `button2` you could keep values on list `all_buttons = [23, 21, ...]` `all_leds = [... ]` or in dictionary - and then you could use `for`-loop ot create the same elements or execute the same functions - and code would be much shorter. – furas Jan 06 '21 at 23:47
  • did you run code in console/terminal to see if this display any error messages ? – furas Jan 06 '21 at 23:50
  • I don't understand why you use `while player == True:` if code inside this loop will be executed only once. There is no need to use loop if it will not loop (run many times). – furas Jan 06 '21 at 23:51

0 Answers0