a bit of background info on what I am running: I have a Raspberry Pi which is listening to serial communication from a Wemos D1. When it receives a message it will play the relevant video file. When it is not playing movie1 or movie2 I want the tvbackground video to be playing on loop. Serial comms can be received at any point and the tvbackground needs to immediately stop playing to allow movie1 or movie2 to begin. When movie1 or movie2 has played through, I want tvbackground to automatically begin again on loop.
I have got things working almost exactly how I would like except for restarting the tvbackground clip. It either (1) Won't restart tvbackground once movie1 or movie2 has finished OR (2) If I add a break in, it begins playing movie1 and then immediately starts the tvbackground before movie1 has finished playing.
Can I prevent the python script from breaking the loop and restarting tvbackground until after movie1 has finished playing, or is there an easier solution?
Thank you!
#!/usr/bin/evn python3
import serial
import time
import sys
import os
from subprocess import Popen
import psutil
movie1 = "/home/pi/Videos/001_1080p.mp4"
movie2 = "/home/pi/Videos/002_1080p.mp4"
tvbackground = "/home/pi/Videos/tvbackground_1080p.mp4"
n = 0
FNULL = open(os.devnull,'w')
def getplayers():
procs = []
for p in psutil.process_iter():
if p.name() == 'omxplayer.bin':
procs.append(p)
return procs
def killoldplayers(procs):
for p in procs:
p.kill()
while True:
players = getplayers()
#THIS WILL AUTOPLAY AND LOOP FOREVER, BUT WON'T REOPEN ONCE ANOTHER VIDEO PLAYS
cmd = "omxplayer --loop --no-keys --layer %d %s "%(n,tvbackground)
Popen(cmd, shell=True, stdout=FNULL,stderr=FNULL)
killoldplayers(players)
if __name__ == '__main__':
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1)
ser.flush()
while True:
players = getplayers()
if ser.in_waiting > 0:
line = ser.readline().decode('utf-8').rstrip()
print(line)
n += 1
if(line=="One"):
cmd = "omxplayer --no-keys --layer %d %s "%(n,movie1)
Popen(cmd, shell=True, stdout=FNULL,stderr=FNULL)
killoldplayers(players)
#IT NEEDS TO RESTART TVBACKGROUND AUTOMATICALLY HERE, BUT ONLY ONCE MOVIE1 HAS FINISHED
elif(line=="Two"):
cmd = "omxplayer --no-keys --layer %d %s "%(n,movie2)
Popen(cmd, shell=True, stdout=FNULL,stderr=FNULL)
killoldplayers(players)
#IT NEEDS TO RESTART TVBACKGROUND AUTOMATICALLY HERE, BUT ONLY ONCE MOVIE2 HAS FINISHED