我试图在播放视频时使用Python脚本控制OMXplayer。我是Python上Dbus的新手,希望只是遗漏了一些简单的东西。在
最终,我想用一个使用GPIO引脚的运动传感器来实现这一点,但首先我只是试图控制播放器。我需要做的是循环视频的一个部分一段时间过去,然后跳到'第二'节时,该段中断。我正在用一个密钥项来测试这个。在
我的问题是代码中的“if”语句不会循环,除非输入的键或任何其他信号中断。任何键都会导致中断,但我希望在不输入任何输入的情况下触发循环中特定于时间的“if”语句。我希望我所说的在下面的代码中是清楚的,但如果不是,请问任何问题,我将很高兴尝试澄清。在
我的硬件是树莓皮3B+
我已经清除了代码中不必要的部分,所以这是一个基本的功能版本。在from omxplayer.player import OMXPlayer #runs from the popcornmix omxplayer wrapper at https://github.com/popcornmix/omxplayerhttps://github.com/popcornmix/omxplayer and https://python-omxplayer-wrapper.readthedocs.io/en/latest/)
from pathlib import Path
import time
import RPi.GPIO as GPIO #for taking signal from GPIO
import subprocess
import logging
logging.basicConfig(level=logging.INFO)
VIDEO_PATH = Path("VIDEO.m4v")
player_log = logging.getLogger("Player 1")
player = OMXPlayer(VIDEO_PATH, dbus_name='org.mpris.MediaPlayer2.omxplayer1')
player.playEvent += lambda _: player_log.info("Play")
player.pauseEvent += lambda _: player_log.info("Pause")
player.stopEvent += lambda _: player_log.info("Stop")
positionEvent = 12
flyin = 3
flyaway = 15
'''
THE ERROR OCCURS IN THE BELOW FUNCTION!!!
the following function does not run each time
the 'while' loop below is playing, but does run
when there is a key pressed as an 'input'
I want to add a chck so that each time a certain
amount of time passes, the video jumps back
'''
def checktime():
currtime = player.position()
print("current time is " + str(currtime))
if(currtime > 3):
print("currtime is greater than 3")
try:
player.play()
print (" Ready")
while True:
'''
Below is the call for the function 'checktime'
This runs once when the video starts, and only
runs again when a key is entered.
I want this to run on a continuous loop
and call each time the video loops
'''
checktime()
key = input()
if key == 'd': #works perfectly
currtime = player.position()
player.seek(flyin-currtime)
print("player position is" + str(player.position()))
if key == 'a': #works perfectly
currtime = player.position()
player.seek(flyaway-currtime)
if key == 's': #works perfectly
player.play()
'''
in addition to the key inputs above, entering any key
and pressing return will run the checktime()
function. I understand that this is receiving an
input, therefore 'waking' the program up,
but I don't understand why the loop is not working
continuously
'''
# Wait for 10 milliseconds
time.sleep(.01)
except keyboardInterrupt:
print (" Quit")
# Reset GPIO settings
GPIO.cleanup()
视频是用's'、'a'和'd'键作为输入进行控制的,这是完美的。但是,“checktime()”函数不会在每次while循环循环时调用。我的猜测是,这是因为一旦视频播放,在接收到另一个输入之前,它不会从Python程序中寻找任何东西。在
我不确定我是否正确地使用了DBus。理想情况下,我想使用omxplayer包装器的seek_absolute函数,但我无法理解这一点。在这个项目上我一直在努力,并且自从发布了一个早期的问题(见这里:OMXplayer-wrapper and Python - jump to a specific point in video)以来,我已经有了更好的理解,但我在这里有点困惑。在
如果有人能帮忙,我会很感激你的建议!在