Video stimuli in loops

I am new to using Psychopy, and have been working with a combination of the builder and coder views. I’m pretty new to coding as well.

I am trying to generate a loop where one of two types of videos can be played based on which key is pressed. For the particular experiment, there are videos where a person looks sad (3 versions) and where a person looks happy (3 versions). I want to be able to press a key and have one of the sad videos play, or press a key and have a happy video play.

I set up a conditions file with “happy” and “sad” conditions, each with a list of 3 videos. And set the loop to “full random”. This seems to be working for a few rounds of the loop, except it keeps crashing. The screen freezes and no key responses seem to work. Sometimes it crashes and exits out of the program too. Does anyone have any suggestions of how to get this to stop crashing?

Here is the code I have for this part of the loop:

------Prepare to start Routine “response”-------

t = 0
responseClock.reset()  # clock
frameN = -1
continueRoutine = True
# update component parameters for each repeat
sadvid = visual.MovieStim3(
    win=win, name='sadvid',
    noAudio = False,
    filename=sad,
    ori=0, pos=(0, 0), opacity=1,
    size=[1440,900],
    depth=0.0,
    )
happyvid = visual.MovieStim3(
    win=win, name='happyvid',
    noAudio = False,
    filename=happy,
    ori=0, pos=(0, 0), opacity=1,
    size=[1440,900],
    depth=-1.0,
    )
recog_key = event.waitKeys(maxWait = 100000,keyList = ['a','l']) 
# keep track of which components have finished
responseComponents = [sadvid, happyvid, recog_key]
for thisComponent in responseComponents:
    if hasattr(thisComponent, 'status'):
        thisComponent.status = NOT_STARTED

# -------Start Routine "response"-------
while continueRoutine:
    # get current time
    t = responseClock.getTime()
    frameN = frameN + 1  # number of completed frames (so 0 is the first frame)
    # update/draw components on each frame
    
    # *sadvid* updates
    if recog_key[0][0] == 'a':
        # keep track of start time/frame for later
        sadvid.tStart = t
        sadvid.frameNStart = frameN  # exact frame index
        sadvid.setAutoDraw(True)
    frameRemains = 0.0 + 10- win.monitorFramePeriod * 0.75  # most of one frame period left
    if sadvid.status == STARTED and t >= frameRemains:
        sadvid.setAutoDraw(False)
    if sadvid.status == FINISHED:  # force-end the routine
        continueRoutine = False
        
    # *happyvid* updates
    if recog_key[0][0] == 'l':
        # keep track of start time/frame for later
        happyvid.tStart = t
        happyvid.frameNStart = frameN  # exact frame index
        happyvid.setAutoDraw(True)
    frameRemains = 0.0 + 10- win.monitorFramePeriod * 0.75  # most of one frame period left
    if happyvid.status == STARTED and t >= frameRemains:
        happyvid.setAutoDraw(False)
    if happyvid.status == FINISHED:  # force-end the routine
        continueRoutine = False
    
    # check if all components have finished
    if not continueRoutine:  # a component has requested a forced-end of Routine
        break
    continueRoutine = False  # will revert to True if at least one component still running
    for thisComponent in responseComponents:
        if hasattr(thisComponent, "status") and thisComponent.status != FINISHED:
            continueRoutine = True
            break  # at least one component has not yet finished
    
    # check for quit (the Esc key)
    if endExpNow or event.getKeys(keyList=["escape"]):
        core.quit()
    
    # refresh the screen
    if continueRoutine:  # don't flip if this routine is over or we'll get a blank screen
        win.flip()

# -------Ending Routine "response"-------
for thisComponent in responseComponents:
    if hasattr(thisComponent, "setAutoDraw"):
        thisComponent.setAutoDraw(False)
# the Routine "response" was not non-slip safe, so reset the non-slip timer
routineTimer.reset()

Thanks for any help!

Sarah