TimmyBlue
Posts: 7
Joined: Wed Nov 05, 2014 10:25 am

Video in Loop + One Video on pressed Button

Tue Nov 18, 2014 8:31 am

Dear Community,
I need a simple Application which makes this possible:
One Video has to be played in loop - all the time using the omx player.

On pressing a Button using GPIO ANOTHER Video has to be played (a single time), after this video, the other looped video has to be player.
I wrote this script, but nothing happens by pressing the GPIO...

Is there someone who can help me?

Code: Select all

import thread
import time
import RPi.GPIO as GPIO
import sys
import subprocess
import os
import glob

GPIO.setmode(GPIO.BOARD)
GPIO.setup(18, GPIO.IN)
GPIO.setup(11, GPIO.OUT)

def hintergrund():
while True:
path ='/media/usb/Media/'
for infile in glob.glob(os.path.join(path, 'test.mp4')):
a = subprocess.call( [ "omxplayer", "-o", "hdmi", infile])

def front():
while True:
path ='/media/usb/Front/'
for infile in glob.glob(os.path.join(path, 'test.mp4')):
a = subprocess.call( [ "omxplayer", "-o", "hdmi", infile])

def gpio():
if GPIO.input(18) == 1:
return 1
else:
break

while True:
check = gpio()
if check == 1:
front()
else:
hintergrund()

User avatar
paddyg
Posts: 2554
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Video in Loop + One Video on pressed Button

Tue Nov 18, 2014 10:41 am

hard to tell without indents but it looks like you are starting lots of subprocesses all at the same time in those for loops. You need to think about what happens when you press and when you don't press the GPIO button. At the moment your function doesn't define explicitly what to return when the button isn't pressed but if the button is pressed you will get lots (as there isn't a sleep in the loop) of 1s back and front() will be called repeatedly. You need some way of remembering that you have already pressed the button.

ED plus both your playing functions look inescapable.

I would have thought you could start omxplayer once and pass instructions to it like KenT does here
https://github.com/KenT2/pyomxplayer/bl ... xplayer.py
or even just use something like pypresents which possibly does what you want already.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

TimmyBlue
Posts: 7
Joined: Wed Nov 05, 2014 10:25 am

Re: Video in Loop + One Video on pressed Button

Tue Nov 18, 2014 10:50 am

Hey paddyg,
yeah I thought about that a few Minutes ago.
I just wrote this script with a "buttonpressed" variable (global)
The Problem here will be, The loop will never stop to play the other vid...

Code: Select all

from threadding import Thread
from Queue import Queue
import RPi.GPIO as GPIO
import sys
import subprocess
import os
import glob
global schalterindex
buttonpressed = 0

GPIO.setmode(GPIO.board)
GPIO.setup(18, GPIO.IN)
GPIO.setup(11, GPIO.OUT)

def video(queue):
    while True:
        if buttonpressed == 1:
            path ='/media/usb/Media/'
            for infile in glob.glob(os.path.join(path, 'test.mp4')):
                a = subprocess.call( [ "omxplayer", "-o", "hdmi", infile])
        else:
            #Second Video Simple Play, currently not here... Test with LED 
            GPIO.output(11, GPIO.HIGH)
            time.sleep(0.1)
            GPIO.output(11, GPIO.LOW)
            time.sleep(0.1)schalterindex = 0

def gpio(queue):
    while True:
        if GPIO.input (18) == 1:
            buttonpressed = 0

queue = Queue()
gpio = Thread(target=gpio, args=(queue,))
gpio.start
video = Thread(target=video, args=(queue,))
video.start

User avatar
paddyg
Posts: 2554
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Video in Loop + One Video on pressed Button

Tue Nov 18, 2014 11:37 am

OK. You still have the issue of starting omxplayer repeatedly. Your infinite loop in video() makes more sense if it's in a thread but a) you need to make it a thread that will be killed when the program exits (daemon = True or something (look it up)) b) You need to stop your main program dropping off the end, i.e. put a main loop in (with a small sleep). Also you will find that setting a value to a global variable will not work unless you explicitly define it as global using 'global buttonpressed'. I'm not sure how you intend to use you queues either. I still suggest you study how to start omxplayer once and pass controls to it using pipes (see earlier link) and avoid using threads unless you absolutely have to.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

PiGraham
Posts: 3971
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: Video in Loop + One Video on pressed Button

Tue Nov 18, 2014 11:48 am

You'll either need to wait for the sub-processes to complete, or kill them when no longer needed, and, as Paddyg says, don't start multiple instances.

Something like:

Code: Select all

if buttonpressed and not waspressed
    if backvideo playing
        kill backvideo
    start front video
waspressed = buttonpressed


Somewhere else:

if frontvideo finished and backvideo finished
    start backvideo

TimmyBlue
Posts: 7
Joined: Wed Nov 05, 2014 10:25 am

Re: Video in Loop + One Video on pressed Button

Tue Nov 18, 2014 11:58 am

Thank you guys!
My biggest problem is how to kill the background-video in the omx player. I am really new with Python programming (Only one week :roll: ) but do my best effort to learn.

Return to “Python”