willprice / python-omxplayer-wrapper

:tv: Control OMXPlayer, the Raspberry Pi media player, from Python
http://python-omxplayer-wrapper.readthedocs.io
GNU Lesser General Public License v3.0
253 stars 71 forks source link

NameError: name 'player' is not defined #148

Closed theZweck closed 5 years ago

theZweck commented 5 years ago

Issue Report

Description

I wrote a basic program for controlling omxplayer via GPIO buttons through the wrapper. I'm using Popen to play a video file but when I test the play/pause button during playback using player.pause(), I get the error: "NameError: name 'player' is not defined". (I would use the wrapper for all control functions but it doesn't work.) I've checked countless posts on many different forums and tried many things that would likely have worked but too advanced for me, including attempting to integrate dbus manually but that's way over my head at this time. My skill level with the Pi and python is at the general hobbyist level so explicit code and terminal commands with simple instructions and explanations are ideal. I wanted a simple library to work with but it's become a headache....

Problem reproduction

from subprocess import Popen from omxplayer.player import OMXPlayer

GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM)

playPause = 14 play_led = 2

GPIO.setup(playPause, GPIO.IN) GPIO.setup(play_led, GPIO.OUT)

vFile = '/media/pi/MEDIACARD/*.mp4 cmd = "omxplayer %s" %(vFile)

while True: if GPIO.input(playPause) == False: time.sleep(0.2) Popen([cmd], shell = True) GPIO.output(play_led, True) if GPIO.output(playPause) == False: player.pause()

Environment details

Software Version
python-omxplayer-wrapper 0.2.5
python-dbus (dpkg -s python-dbus) 1.2.4-1
python (python --version) 2.7.13
omxplayer (omxplayer --version) 5a25a57
jehutting commented 5 years ago

Hi,

You are mixing some things up :-)

You don't need Popen. All that kind of stuff is wrapped in the OMXPlayer wrapper.

The reason for the NameError: name 'player' is not defined error is that you haven't created an instance called player of the (wrapper) class OMXPlayer:

player = OMXPlayer(vFile)

Once having this instance you can control the real OMXPlayer with the wrapper instance functions like

player.pause()
player.play() 
player.quit()

In your case you also need the is_playing() function. With this function you can determine if the player is playing or not. So when you press your button playPause check the status and decide from its value to either call pause() or play().

theZweck commented 5 years ago

The reason I didn't use player = OMXPlayer(vFile) to start was because I tried but upon pressing my play button I got SystemError: DBus cannot connect to the OMXPlayer process. (I tried again for kicks but the issue remains.) I tried finding a fix for it but all the posts I read went way over my head.

jehutting commented 5 years ago

OK... so you have the same problem as reported in issue #125.

Can you post the output of the commandline command uname -a? Also the output of cat /etc/os-release.

When you run the real OMXPlayer, are you able to pause it with the SPACE keyboard button?

Hmmm... I now see that you have an asterisk * in your vFile variable.

vFile = '/media/pi/MEDIACARD/*.mp4

Both the real and the wrapper OMXPlayer are not able to handle a list of files; just one.

So try the following (test) script

#!/usr/bin/env python3

from omxplayer.player import OMXPlayer
from pathlib import Path
from time import sleep

VIDEO_PATH = Path("/path/to/a/single/file.mp4")  # <<< REPLACE THIS BY ONE OF YOUR FILES!

player = OMXPlayer(VIDEO_PATH)

sleep(5)

player.quit()

While you are trying things out, perhaps you could listen to some music with omxplayer-player.

willprice commented 5 years ago

Thanks for the comments @jehutting