psychopy.org | Reference | Downloads | Github

Video Stimulus Involving a Password and Dialogue Box to Get Video to Play

OS: Windows 10
PsychoPy version (e.g. 1.84.x): 1.84
Standard Standalone? (y/n): Yes
What are you trying to achieve?: I need to be able to run a video, pause it multiple times, then in order to play it the user has to be able the user has to enter in the correct password via a dialogue box when prompted. The title of the dialogue box displays the password. The timing on the box works on a timer function. I don’t need to record the user input or anything like that. This is essentially a check to make sure that they’re paying attention to the stimulus in order to complete the task. I need to be able to achieve 4 different pauses in my entire video, and the video is ~10 minutes, but I think it should work as long as I can achieve 2 pause play loops involving dialogue boxes.

What did you try to make it work?: I’ve reworked this program a number of times and I believe I’m close, but I have no idea. I haven’t been able to get the video to play, pause, password prompt, then play successfully a second time after the first pass through.

What specifically went wrong when you tried that?: Right now, the program is currently in an infinite loop and will not run. So don’t run it on your own computer. I believe I’ve figured out where the issues are, and I’ve commented those out.

My code is listed below:
#!/usr/bin/env python2
# -- coding: utf-8 --
from future import absolute_import, division
from psychopy import locale_setup, gui, visual, core, data, event, logging, sound
import time
import os
import sys
from timeit import default_timer

videopath = r'./Devin.mp4'
videopath = os.path.join(os.getcwd(), videopath)
if not os.path.exists(videopath):
    raise RuntimeError("Video File could not be found:" + videopath)
INCLUDE_AUDIO_TRACK = True
win = visual.Window([1024, 768])
USE_FULLSCREEN_WINDOW = True #Needs to be full screen
mov = videopath

video_name = r'./Devin.mp4'

def getVideoFilePath(): 
videopath = os.path.normpath(os.path.join(os.getcwd(), video_name))
if not os.path.exists(videopath): 
    raise RuntimeError("Video File could not be found:" + videopath)
return videopath


if __name__ == '__main__': #I think there's an issue here

    win = visual.Window(WINDOW_SIZE, fullscr=USE_FULLSCREEN_WINDOW, allowGUI=not USE_FULLSCREEN_WINDOW, 
                        screen=SCREEN_NUMBER)

    # Create your movie stim.
    mov = visual.MovieStim2(win, getVideoFilePath(), 
                            size=None,  # (1280, 720), 
                            # pos specifies the /center/ of the movie stim location
                            pos=[0, 0], 
                            flipVert=False, 
                            flipHoriz=False, 
                            noAudio=not INCLUDE_AUDIO_TRACK, 
                            # fps=90, 
                            loop=False)

    while mov.status != visual.FINISHED: #Play until visual is not finished, could be issue here
        mov.play()
        win.flip()
        start = default_timer() #Starting the timer function
        duration = default_timer() - start
        if duration == 45:
            mov.pause #Pausing at 45 seconds
            myDlg = gui.Dlg(title='The password is: dog') #dialogue prompting for password
            myDlg.addField('Please enter the password:')
            myDlg.show()
            if myDlg.OK:
                response = myDlg.data[0]
                if response == 'dog':
                    mov.play()
                    win.flip()
                while myDlg != myDlg.OK or response != 'dog': #If user gets password wrong, or cancels, display dialogue box indefinitely
                    myDlg.show()
                    if myDlg.OK:
                        response = myDlg.data[0]
                        if response == 'dog':
                            mov.play()
                            win.flip()
                            
        start = 0 #Second loop of the above logic after resetting the timer to 0, so it should occur at 90 seconds into the video
        start = default_timer() - start
        if duration == 45:
            mov.pause
            myDlg = gui.Dlg(title='The password is: cat')
            myDlg.addField('Please enter the password:')
            myDlg.show()
            if myDlg.OK:
                response = myDlg.data[0]
                if response == 'cat':
                    mov.play()
                    win.flip()
                while myDlg != myDlg.OK or response != 'cat':
                    myDlg.show()
                    if myDlg.OK:
                        response = myDlg.data[0]
                        if response == 'cat':
                            mov.play()
                            win.flip()
                            
    


win.close()
core.quit()

For a start (and this applies to all programming languages), never test for equality with floating point numbers. i.e. it makes sense to test if an integer == 45, but not for a floating point number. A floating point number (as in the measured time here) is unlikely to ever be exactly equal to 45. You need to test for when it first equals or exceeds 45, for example.

But that may be unrelated to why you are an infinite loop. What evidence do you have that is occurring? Where is it caught?

And why do you have if __name__ == '__main__': bit in this script?