psychopy.org | Reference | Downloads | Github

Coding for Movie Stimulus

movies
#1

Hello,

I am trying to encode a movie stimulus. I know that the builder view has a movie component, but I am trying to add the movie in a routine that contains a 2-back task. I have found that certain keys, such as pausing, attributed to the movie reset themselves when the letters in the task change. So, I have decided to try to code the movie stimulus into the routine (within Builder) in hope that it may run continuously and the keys remain operable throughout the duration of the task.

However, I have found that the coding that I have tried to use from both the demos and previous responses to my other topics allows the audio to play but the movie will not actually appear on the screen. I do not have any experience with coding, so I am not certain what the issue could be. If there is anyone who could point out which part of coding needs to be corrected or what needs to added so that movie will play correctly, it will be much appreciated. Thank you.

from psychopy.constants import (PLAYING, PAUSED)
from psychopy import prefs 
prefs.hardware['audioLib'] = ['sounddevice']
from psychopy import visual, core, event
import time, os

videopath = r'C:\Users\\alezu\\Downloads\\MattCutts_2011U-480p.mp4'
videopath = os.path.join(os.getcwd(), videopath)
if not os.path.exists(videopath):
   raise RuntimeError("Video File could not be found:" + videopath)

win = visual.Window([1366, 768])

mov = visual.MovieStim3(
    win, videopath,
    size=[555,200], pos=[-250, 250],
    flipVert=False, flipHoriz=False,
    loop=False)

shouldflip = mov.play()

#Check for action keys.....
for key in event.getKeys():
    if key in ['escape', 'q']:
        win.close()
        core.quit()
    elif key in ['p', ]:
        # To pause the movie while it is playing....
        if mov.status == PLAYING:
            print('PAUSING')
            mov.pause()
        elif mov.status == PAUSED:
        # To /unpause/ the movie if pause has been called....
            print('UN-PAUSING')
            mov.play()
0 Likes

#2

Hi,

so you want to play a movie stimulus across another routine, right?

In your code, the movie is never drawn.

If you use Builder, I suggest using as much of the Builder components as possible and only manipulate the components with your script.

In your case, create a routine before your 2-back task to initialize the movie, and start it with a code component right after the routine has finished (set it to autodraw). Then in your 2-back task add a code component to play and pause the movie. After the 2-back task add another routine to stop the movie (set autodraw to false).

Please download the following psyexp for a working example:

movie-across-routine.psyexp (11.1 KB)

After opening the psyexp, click on the “start_video” routine, and replace the file name “add-your-movie-here.mp4” in the mylongvideo component with your movie file.

Please note that this is a quick and dirty solution and you might need additional code to ensure that the movie component releases its memory in case you are using multiple movies across your experiment.

All the best,

Frank.

0 Likes

#3

Thank you so much, Frank! Your solution worked out great as the movie now plays and pauses without any of the issues that I have encountered before.

The experiment will be using the same movie file but in a counterbalanced order (movie shown during task and not shown during task) so it may not be an issue.

0 Likes