title: How to open and close omxplayer (Python/Raspberry Pi) while playing video?
body: <p>Using a Raspberry Pi and some push buttons I want to control video playback. When someone presses a button the corresponding video plays. The buttons work great. When you press a button the video plays, but when you press a different button or the same button it opens the video without closing the video that was currently playing. I've been searching a while to fix this. I'm pretty new to Python so please keep it as simple as possible. In the code below I'm trying to accomplish it using multithreading. I wasn't able to close the thread though when another thread starts. I am able to close the video after it plays for 10 seconds, but I can't move the quit command anywhere else to close other videos: playSippycup.stdin.write('q')</p>
  
  <p>Here's are the errors I'm currently receiving:</p>
  
  <pre><code>Unhandled exception in thread started by &lt;function shoppingcart at 0xb6c566f0&gt;Playing Sippycup
  
  Unhandled exception in thread started by &lt;function dodgeballs at 0xb6c56670&gt;
  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
  </code></pre>
  
  <p>Thanks so much for any help you guys can offer!</p>
  
  <pre><code>#!/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()
  </code></pre>
  
  <hr>
  
  <p><strong>NEW EDIT:</strong></p>
  
  <pre><code>#!/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()
  </code></pre>
  
  <p><strong>ERROR:</strong></p>
  
  <p>Unhandled exception in thread started by 
  Traceback (most recent call last):
    File "./labmural2.py", line 28, in welcome_loop
      playProcess.stdin.write('q')
  IOError: [Errno 32] Broken pipe</p>