1

I am trying to play a random video file upon an input from a PIR sensor.

Rasberry Pi 3, running Rasbian.

Problem is, I don't know what I'm doing and just trying to hack together different files.

So far, I have the PIR detection script (pirDetect) working, and the playback file also works, except instead of playing back a random file in the directory, it plays back the same one over and over on every detection.

Here is what I am using at the moment:

#!/usr/bin/python
import subprocess as sp
import time
from pirDetect import *
import random
import os

directory = "."

video = ["omxplayer", "filename", "-o", "local"]
scareFile = random.choice(os.listdir(directory))

def onMotion(currState):
    if currState:
            video[1] = scareFile
            subVideo = sp.Popen(video)
            while subVideo.poll() is None:
                    time.sleep(.1)

objDetect = detector(7)
objDetect.subscribe(onMotion)
objDetect.start()

Thanks in advance for any help you can provide.

2 Answers 2

4

According to your code the value of scarefile is just initialized once. since you need a different file to be selected every time motion is detected a you need to pick a new file from within the onMotion function like so

def onMotion(currState):
  if currState:
    scareFile = random.choice(os.listdir(directory))
    video[1] = scareFile
    subVideo = sp.Popen(video)
    while subVideo.poll() is None:
      time.sleep(.1)
1
  • That is working like a charm. Thank you. I wish I knew how to troubleshoot like that.
    – Joelamite
    Commented Oct 15, 2016 at 23:30
0

the random file is selected once in the beginning of the script, then it doesn't get changed anymore. you need to call the choice function each time the omxplayer is called.

1
  • That makes sense. When I run the script after stopping it, it will play a different file over and over. I'll give that a try.
    – Joelamite
    Commented Oct 15, 2016 at 23:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.