GPIO 结合 OMX Player 触发时播放不同的视频

Posted

技术标签:

【中文标题】GPIO 结合 OMX Player 触发时播放不同的视频【英文标题】:GPIO combine with OMX Player to play different videos when triggered 【发布时间】:2019-04-04 11:10:53 【问题描述】:

我有一个简单的项目,我想结合运动传感器来播放某些视频文件。所以通常在不定式循环中,我想播放一个闪烁的视频,如果触发了运动传感器,我想停止闪烁的视频并选择一个可怕的视频。请参阅以下代码。

import RPi.GPIO as GPIO
import time
from omxplayer.player import OMXPlayer
from random import randint

#
def main():
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(17,GPIO.IN)

    flicks = ("/home/pi/halloween/flicks/tv_noise_1.mp4")
    scares = ("/home/pi/halloween/scares/tv_noise_kitten_zombie_2.mp4")

    omxc = OMXPlayer(flicks)
    state = 0 # set initial state as 0
    while True:
        i = GPIO.input(17)
        if(not omxc.is_playing()):
            omxc = OMXPlayer(flicks)
        if(state != i): # change in state detected
            omxc.quit()
            omxc = OMXPlayer(scares)
            time.sleep(35) # wait as long as this video lasts

        state = i

if __name__ == '__main__':
    main() 

但是,我不断收到错误消息:

Traceback (most recent call last):
  File "scare_old.py", line 29, in <module>
    main()
  File "scare_old.py", line 20, in main
    if(not omxc.is_playing()):
  File "<decorator-gen-90>", line 2, in is_playing
  File "/home/pi/.local/lib/python2.7/site-packages/omxplayer/player.py", line 48, in wrapped
    raise OMXPlayerDeadError('Process is no longer alive, can\'t run command')
omxplayer.player.OMXPlayerDeadError: Process is no longer alive, can't run command

究竟是什么问题?我的意思是该进程应该运行,因为我之前刚刚启动它?

【问题讨论】:

【参考方案1】:
import RPi.GPIO as GPIO
import time
from random import randint
from subprocess import Popen, PIPE
import os
import logging

logger = logging.getLogger(__name__)

class Player:
def __init__(self, movie):
    self.movie = movie
    self.process = None

def start(self):
    self.stop()
    self.process = Popen(['omxplayer', self.movie], stdin=PIPE,
    stdout=open(os.devnull, 'r+b', 0), close_fds=True, bufsize=0)
    self.process.stdin.write("b") # start playing

def stop(self):
    p = self.process
    if p is not None:
        try:
            p.stdin.write("q") # send quit command
            p.terminate()
            p.wait() # -> move into background thread if necessary
        except EnvironmentError as e:
            logger.error("can't stop %s: %s", self.movie, e)

def main():
GPIO.setmode(GPIO.BCM)
GPIO.setup(17,GPIO.IN)

first = ("/home/pi/halloween/flicks/tv_noise_no_sound_short_1.mp4")
second = ("/home/pi/halloween/scares/tv_noise_the_ring_2.mp4")

first_player = Player(movie=first)
second_player = Player(movie=second)

second_player.start()
second_player.stop()

first_player.start()
print "start the first/standard clip"

state = GPIO.input(17)  # get the initial state of PIR
while True:
    i = GPIO.input(17)
    if((first_player.process.poll() is not None) and (second_player.process.poll() is not None)):
            print "restart flickering video"
            first_player.stop()
            second_player.stop()
            first_player.start()
    if(state != i): # change in state detected
        print "state has changed"
        print "scaring video played"
        if(second_player.process.poll() is not None):
            first_player.stop()
            second_player.start()
    state = i

if __name__ == '__main__':
    main()

【讨论】:

以上是关于GPIO 结合 OMX Player 触发时播放不同的视频的主要内容,如果未能解决你的问题,请参考以下文章

Youtube Player API Ended 状态从未触发

OpenMAX (OMX)框架

flutter video_player 在发布模式下不播放视频

Spotify 应用程序:更改位置时触发功能

在视频播放期间以特定时间戳触发事件

Music+ Player的隐私声明