I play 3 videos using MovieStim2 in PsychoPy. I suspect this issue stems from not really understanding loops, though.
I want to skip to the next video when the participant presses a key. I understand how my code quits when the participant presses "q", but I'm not sure how to make it skip to the next video when they press "b", for example. Here is my current code:
vidNum = 1
for f in ['clip1.mpg', 'clip2.mpg', 'clip3.mpg']:
clock.reset()
#logfile.write("AfterForLoopTime,%s,Video %s\n" % (core.getTime(), vidNum))
# Create your movie stim.
mov = visual.MovieStim2(win, videopath+f,
size=640,
# pos specifies the /center/ of the movie stim location
pos=[0, 0],
flipVert=False,
flipHoriz=False,
) # loop=False - need to comment this to use a loop
# Start the movie stim by preparing it to play
shouldflip = mov.play()
logfile.write("AfterShouldflipLine58,%s, Video %s\n" % (clock.getTime(), vidNum))
while mov.status != visual.FINISHED:
# Only flip when a new frame should be displayed. Can significantly reduce
# CPU usage. This only makes sense if the movie is the only /dynamic/ stim
# displayed.
if shouldflip:
# Movie has already been drawn , so just draw text stim and flip
#text.draw()
win.flip()
else:
# Give the OS a break if a flip is not needed
time.sleep(0.001)
# Drawn movie stim again. Updating of movie stim frames as necessary
# is handled internally.
shouldflip = mov.draw()
# Check for action keys.....
for key in event.getKeys():
if key in ['escape', 'q']:
win.close()
core.quit()
I tried adding code similar to the last chunk which quits after q:
elif key in ['b']:
break
but I realize that I really want to break out of this loop:
for f in ['clip1.mpg', 'clip2.mpg', 'clip3.mpg']:
However, this doesn't seem to work either
for f in ['clip1.mpg', 'clip2.mpg', 'clip3.mpg']:
for key in event.getKeys():
if key in ['b']:
break
The loop you actually want to break
is:
while mov.status != visual.FINISHED:
The easiest way I can think of is to just set your movie status to -1
(or visual.FINISHED
) if the user hits the key.
For example:
if key in ['b']:
mov.status = -1
This will break you out of your current video from what I can tell.