Comment ouvrir et fermer omxplayer (Python/Raspberry Pi) lors de la lecture de la vidéo?

À l'aide d'un Raspberry Pi et quelques boutons, je veux contrôler la lecture vidéo. Lorsque quelqu'un appuie sur un bouton, le correspondant de lecture de la vidéo. Les boutons de l'excellent travail. Lorsque vous appuyez sur un bouton de lecture de la vidéo, mais lorsque vous appuyez sur un autre bouton ou sur le même bouton, il ouvre la vidéo sans la fermeture de la vidéo en cours de lecture. J'ai cherché un certain temps à résoudre ce problème. Je suis assez nouveau à Python, donc s'il vous plaît garder aussi simple que possible. Dans le code ci-dessous je suis en train de l'accomplir à l'utilisation du multithreading. Je n'ai pas pu fermer le thread, mais quand un autre thread démarre. Je suis en mesure de fermer la vidéo après il joue pendant 10 secondes, mais je ne peux pas déplacer la commande quit ailleurs de fermer les autres vidéos: playSippycup.stdin.write('q')

Ici sont les erreurs que je suis en train de réception:

Unhandled exception in thread started by <function shoppingcart at 0xb6c566f0>Playing Sippycup

Unhandled exception in thread started by <function dodgeballs at 0xb6c56670>
Traceback (most recent call last):
    File "./labmural2.py", line 53, in dodgeballs
    playDodgeballs.stdin.write('q')
NameError: global name 'playDodgeballs' is not defined
Traceback (most recent call last):
  File "./labmural2.py", line 71, in shoppingcart
    playShoppingcart.stdin.write('q')
NameError: global name 'playShoppingcart' is not defined

Merci beaucoup pour toute aide que vous les gars peut vous offrir!

#!/usr/bin/env python
import RPi.GPIO as GPIO
import subprocess
import time
import thread
GPIO.setmode (GPIO.BCM)
GPIO.setwarnings (False)
GPIO.setup(9, GPIO.IN)
GPIO.setup(10, GPIO.IN)
GPIO.setup(11, GPIO.IN)
GPIO.setup(17, GPIO.OUT)
GPIO.setup(22, GPIO.OUT)
GPIO.setup(27, GPIO.OUT)
def sippycup( name ):
global playSippycup
while True:
if GPIO.input(11) == True:
print name
time.sleep(1)
playSippycup=subprocess.Popen(['omxplayer','Desktop/videos/sippycup.mp4'],stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE, close_fds=True)
time.sleep(10)
playSippycup.stdin.write('q')
if GPIO.input(9) == True:
playSippycup.stdin.write('q')
if GPIO.input(10) == True:
playSippycup.stdin.write('q')
def dodgeballs( name ):
global playDodgeballs
while True:
if GPIO.input(9) == True:
print name
time.sleep(1)
playDodgeballs=subprocess.Popen(['omxplayer','Desktop/videos/dodgeballs.mp4'],stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE, close_fds=True)
time.sleep(10)
playDodgeballs.stdin.write('q')
if GPIO.input(10) == True:
playDodgeballs.stdin.write('q')
if GPIO.input(11) == True:
playDodgeballs.stdin.write('q')
def dodgeballs( name ):
global playDodgeballs
while True:
if GPIO.input(9) == True:
print name
time.sleep(1)
playDodgeballs=subprocess.Popen(['omxplayer','Desktop/videos/dodgeballs.mp4'],stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE, close_fds=True)
time.sleep(10)
playDodgeballs.stdin.write('q')
if GPIO.input(10) == True:
playDodgeballs.stdin.write('q')
if GPIO.input(11) == True:
playDodgeballs.stdin.write('q')
def shoppingcart( name ):
global playShoppingcart
while True:
if GPIO.input(10) == True:
print name
time.sleep(1)
playShoppingcart=subprocess.Popen(['omxplayer','Desktop/videos/shoppingcart.mp4'],stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE, close_fds=True)
time.sleep(10)
playShoppingcart.stdin.write('q')
if GPIO.input(9) == True:
playShoppingcart.stdin.write('q')
if GPIO.input(11) == True:
playShoppingcart.stdin.write('q')
thread.start_new_thread( sippycup, ("Playing Sippycup",) )
thread.start_new_thread( dodgeballs, ("Playing Dodgeballs",) )
thread.start_new_thread( shoppingcart, ("Playing Shoppingcart",) )
while True:
pass
GPIO.cleanup()

NOUVELLE EDIT:

#!/usr/bin/python
from time import sleep
import RPi.GPIO as GPIO
import subprocess
import time
import thread
GPIO.setmode (GPIO.BCM)
GPIO.setwarnings (False)
GPIO.setup(9, GPIO.IN)
GPIO.setup(10, GPIO.IN)
GPIO.setup(11, GPIO.IN)
GPIO.setup(17, GPIO.OUT)
GPIO.setup(22, GPIO.OUT)
GPIO.setup(27, GPIO.OUT)
def welcome_loop():
while True:
global playProcess
x = 1
print "LOOPING"
time.sleep(.5)
playProcess=subprocess.Popen(['omxplayer','-b','Desktop/videos/loop/loop.mp4'],stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE, close_fds=True)
time.sleep(10)
playProcess.stdin.write('q')
x += 1
def videos():
while True:
if GPIO.input(9):
print "STOP LOOP"
time.sleep(.5)
playProcess.stdin.write('q')
time.sleep(.5)
print "Play Sippycup"
sippycup_video=subprocess.Popen(['omxplayer','-b','Desktop/videos/sippycup.mp4'],stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE, close_fds=True)
time.sleep(10)
sippycup_video.stdin.write('q')
time.sleep(.5)
welcome_loop()
if GPIO.input(10):
print "STOP LOOP"
time.sleep(.5)
playProcess.stdin.write('q')
time.sleep(.5)
print "Play Dodgeballs"
dodgeballs_video=subprocess.Popen(['omxplayer','-b','Desktop/videos/dodgeballs.mp4'],stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE, close_fds=True)
time.sleep(10)
dodgeballs_video.stdin.write('q')
time.sleep(.5)
welcome_loop()
if GPIO.input(11):
print "STOP LOOP"
time.sleep(.5)
playProcess.stdin.write('q')
time.sleep(.5)
print "Play Shoppingcart"
shoppingcart_video=subprocess.Popen(['omxplayer','-b','Desktop/videos/shoppingcart.mp4'],stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE, close_fds=True)
time.sleep(10)
shoppingcart_video.stdin.write('q')
time.sleep(.5)
welcome_loop()
thread.start_new_thread( videos, () )
thread.start_new_thread( welcome_loop, () )
while True:
pass
GPIO.cleanup()

ERREUR:

Exception non gérée dans le thread lancé par
Traceback (most recent call last):
Le fichier "./labmural2.py", à la ligne 28, dans welcome_loop
playProcess.stdin.write('q')
IOError: [Errno 32] Broken pipe

  • N'utilisez pas de if x == True:, utilisez if x: à la place.
InformationsquelleAutor jmcclaire | 2014-10-15