我编写了一个简单的脚本来在树莓派上按下按钮时播放随机视频。它适用于内置的 Raspberry Pi Python 3 IDE。运行正常
import RPi.GPIO as GPIO
import random
import os
import sys
from time import sleep
from subprocess import call
from omxplayer.player import OMXPlayer
#Uses the board pin number
GPIO.setmode(GPIO.BOARD)
#Delays
Skip_Wait_Time = 1
Genre_Wait_Time = 3
Next_Movie_Wait_Time= 4
#Pin Numbers
Button1_Pin = 35
Button2_Pin = 33
Button3_Pin = 23
Button4_Pin = 24
ShutdownButton_Pin = 21
QuitButton_pin = 37
#Sets Up the 4 GPIO Pins with pull ups
GPIO.setup(Button1_Pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(Button2_Pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(Button3_Pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(Button4_Pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(ShutdownButton_Pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(QuitButton_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
#Action
Genre1 = ("/home/pi/Videos/ActionMovies/")
#Comedy
Genre2 = ("/home/pi/Videos/ComedyMovies/")
#Sitcoms
Genre3 = ("/home/pi/Videos/Sitcoms/")
#Drama
Genre4 = ("/home/pi/Videos/DramaMovies/")
#Christmas
Genre5 = ("/home/pi/Videos/ChristmasMovies/")
#Button States
Button1_State = True
Button2_State = True
Button3_State = True
Button4_State = True
ShutdownButton_state = True
QuitButton_state = True
#variable
Current_Genre = "Genre1"
Current_Genre_Directory = "Null"
Current_Movie_Path = "Null"
episode= "Null"
Time_Remaining = 2
elapsed = 0
Button_Pressed = False
#-------------------------------------------
#---------------- MAIN LOOP ----------------
#-------------------------------------------
while Button_Pressed == False:
Button1_State = GPIO.input(Button1_Pin)
Button2_State = GPIO.input(Button2_Pin)
Button3_State = GPIO.input(Button3_Pin)
Button4_State = GPIO.input(Button4_Pin)
if Button1_State == False:
Current_Genre = "Genre1"
Button_Pressed = True
Current_Genre_Directory = Genre1
if Button2_State == False:
Current_Genre = "Genre2"
Button_Pressed = True
Current_Genre_Directory = Genre2
if Button3_State == False:
Current_Genre = "Genre3"
Button_Pressed = True
Current_Genre_Directory = Genre3
if Button4_State == False:
Current_Genre = "Genre4"
Button_Pressed = True
Current_Genre_Directory = Genre4
episode = random.choice(os.listdir(Current_Genre_Directory))
Current_Movie_Path = Current_Genre_Directory + episode
player = OMXPlayer(Current_Movie_Path)
while ShutdownButton_state == True and QuitButton_state == True:
Button_Pressed = False
elapsed = player.position()
Time_Remaining = (player.duration() - elapsed)
#Reads the state of the buttons
Button1_State = GPIO.input(Button1_Pin)
Button2_State = GPIO.input(Button2_Pin)
Button3_State = GPIO.input(Button3_Pin)
Button4_State = GPIO.input(Button4_Pin)
ShutdownButton_state = GPIO.input(ShutdownButton_Pin)
QuitButton_state = GPIO.input(QuitButton_pin)
if Button1_State == False:
Current_Genre = "Genre1"
Button_Pressed = True
Current_Genre_Directory = Genre1
if Button2_State == False:
Current_Genre = "Genre2"
Button_Pressed = True
Current_Genre_Directory = Genre2
if Button3_State == False:
Current_Genre = "Genre3"
Button_Pressed = True
Current_Genre_Directory = Genre3
if Button4_State == False:
Current_Genre = "Genre4"
Button_Pressed = True
Current_Genre_Directory = Genre4
if Time_Remaining < 1.0 :
episode = random.choice(os.listdir(Current_Genre_Directory))
Current_Movie_Path = Current_Genre_Directory + episode
player.load(Current_Movie_Path, pause=False)
sleep(Next_Movie_Wait_Time)
if Button_Pressed == True:
episode = random.choice(os.listdir(Current_Genre_Directory))
Current_Movie_Path = Current_Genre_Directory + episode
player.load(Current_Movie_Path, pause=False)
sleep(Skip_Wait_Time)
#what happens if quit or shutdown are pressed
if QuitButton_state == False:
player.quit()
GPIO.cleanup()
if ShutdownButton_state == False:
call("sudo shutdown -h now", shell=True)
我的问题是,当我通过命令提示符运行程序时:
sudo python3 /home/pi/RandomMoviePlayer_Rev1.py
我得到错误:
"Traceback (most recent call last):
File "/home/pi/RandomMoviePlayer_Rev1.py", line 7, in <module>
from omxplayer.player import OMXPlayer
ImportError: No module named 'omxplayer'"
我真的不知道该怎么做才能弄清楚发生了什么。我尝试使用 pip 和 pip3 安装 OMXPLAYER
任何帮助将不胜感激