I would like to play a video on the press of a button using the Raspberry Pi.
The code I am using is as follows:
from time import sleep
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
button1=16
button2=12
GPIO.setup(button1,GPIO.IN,pull_up_down=GPIO.PUD_UP)
GPIO.setup(button2,GPIO.IN,pull_up_down=GPIO.PUD_UP)
while(1):
if GPIO.input(button1)==0:
print "Button 1 Was Pressed:"
omxplayer /home/pi/testmp4.mp4
sleep(.1)
if GPIO.input(button2)==0:
print "Button 2 Was Pressed:"
sleep(.1)
When I press button 2 all works ok, When I press button 1 omxplayer doesn’t play the file and I get the following error:
NameError: name ‘omxplayer’ is not defined
I need to know the code to import the omxplayer to play the file.
I don’t need all the controls - just the simplest code to play the file, Thanks.
Thanks for getting in touch here. As we discussed over the phone, you need to include some way of calling the omxplayer software from Python because omxplayer isn’t part of the standard Python module.
There are two ways of doing this. You can either use a system call, which is a bit like your Python script, entering the command into the terminal (behind the scenes of course), and then running it. Or, you can use a module to launch it straight from your script.
Whilst we discussed using a module over the phone, in hindsight, I think that using a system call would be a better way of doing it. I’d recommend following the process that is shown on this project at Hackster.io
That should walk you through everything you need to do, and even includes all of the code for you.
Hi Sam - thanks for the link.
The first line of the code gives me a syntax error and highlights sysimport:
import Rpi.GPIO as GPIOimport sysimport osfrom subprocess import Popen
So bear in mind that example is done using Python 3, so you need to make sure you’re following his process verbatim for launching and writing it. Python 2 probably won’t work which is why you’re getting that error