I'm a bit new to Python and I'm developping an application for a Raspberry Pi where a motion sensor is connected. The idea is to place this Raspberry in a room and on motion detection a video starts playing fullscreen. After the video is done playing I want to let the device sleep for 10 minutes and than activate the motion sensor again to play the same video again. The screen also should stay black after the video so not quiting the vlc application and see the Raspberry Pi Desktop.
So far I tried some code where in one file the fullscreen works, and in the other file the loop works, but bringing the 2 files togheter is giving errors.
As I said I'm very new to coding so it could be the problem is very stupid and the solution very simple. Thanks in advance for the help. Down below my 2 code-files.
Option 1 (fullscreen not working)
from gpiozero import MotionSensor, LED
from time import sleep
import vlc
# creating Instance class object
player = vlc.Instance()
# creating a new media list
media_list = player.media_list_new()
# creating a media player object
media_player = player.media_list_player_new()
# creating a new media
media = player.media_new("/home/pi/MOSA25.mp4")
# adding media to media list
media_list.add_media(media)
# setting media list to the mediaplayer
media_player.set_media_list(media_list)
# setting loop
player.vlm_set_loop("1", True)
#fullscreen
#media_player.set_fullscreen(True)
#Motion Sensor
pir = MotionSensor(4)
#Led
led = LED(26)
led.off()
print("Sensor loading.")
pir.wait_for_no_motion()
sleep(5)
while True:
print("Ready")
pir.wait_for_motion()
print("Motion detected")
led.on()
sleep(5)
led.off()
media_player.play()
sleep(30)
OPTION 2 (Loop not working)
from gpiozero import MotionSensor, LED
from time import sleep
import vlc
# creating Instance class object
vlc_instance = vlc.Instance()
player = vlc_instance.media_player_new()
player.set_mrl("/home/pi/MOSA25.mp4")
player.set_fullscreen(True)
# setting loop
#player.vlm_set_loop("1", True)
#Motion Sensor
pir = MotionSensor(4)
#Led
led = LED(26)
led.off()
print("Sensor wordt geladen.")
pir.wait_for_no_motion()
sleep(5)
while True:
print("Ready")
pir.wait_for_motion()
print("Motion detected")
led.on()
sleep(5)
led.off()
player.play()
sleep(20)
CodePudding user response:
Keep track of the video playing in another while
loop and reload before starting again.
Here's your code (adjusted as I don't have your kit)
from time import sleep
import vlc
playing = set([1,2,3,4])
# creating Instance class object
vlc_instance = vlc.Instance()
player = vlc_instance.media_player_new()
player.set_fullscreen(True)
#Motion Sensor
#pir = MotionSensor(4)
#Led
#led = LED(26)
#led.off()
print("Sensor wordt geladen.")
#pir.wait_for_no_motion()
sleep(5)
while True:
print("Ready")
# pir.wait_for_motion()
sleep(1)
print("Motion detected")
# led.on()
sleep(1)
# led.off()
player.set_mrl("/home/pi/MOSA25.mp4")
player.play()
sleep(1)
while player.get_state() in playing:
sleep(1)
continue
print("Finished")
sleep(5)
continue