psychopy.org | Reference | Downloads | Github

How to close the window at the end of Movie stim

I have to create an experiment with video and a key press after the video but when i’m testing the code it didn’t close the window at the end ofthe video and it is written “memory error”
this is my code:

from psychopy import visual , event, core
win = visual.Window([600,600])
mov = visual.MovieStim(win, ‘test.mov’,size = (600,400))
globalClock = core.Clock()
while mov.status != visual.FINISHED:
mov.draw()
win.flip()
if event.getKeys() :
break
win.close()
core.quit()
untitled_lastrun.py (5.5 KB)

Copy or post a screenshot of the complete error message, and your sample code is unformatted and needs to have the correct indentation for people to help. Please put three backticks (```) on the line before and the line after your code when posting here so we can read it, and it comes out looking like this:

from psychopy import visual , event, core
win = visual.Window([600,600])
mov = visual.MovieStim(win, 'test.mov',size = (600,400))
globalClock = core.Clock()
while mov.status != visual.FINISHED:
    mov.draw()
    win.flip()
    if event.getKeys() :
        break
win.close()
core.quit()

Try this (keeping in mind I’ve never used movies in psychopy):

Add this line after your first line:

from psychopy.constants import FINISHED

or this instead:

from psychopy.constants import *

Then change your ‘while’ condition to:

while mov.status != FINISHED:

And I think the win.close() call is unnecessary when you call core.quit().

Unless I’m wrong, the issue is that visual.FINISHED isn’t actually a thing, so your while condition will never be False, and will never end. That’s why you ran out of memory.

Window.__del__() invokes Window.close(), and core.quit() executes sys.exit(), which does not guarantee that any object’s __del__() method – and, hence, Window.close() – actually gets called. Since there’s some cleanup work going on in Window.close(), it’s advisable to always call it before calling core.quit() to ensure “proper” shutdown.

No, this is not true. Not only do constants.FINISHED and visual.FINISHED have the same value, but they’re actually identical objects:


In [1]: from psychopy import constants, visual

In [2]: constants.FINISHED == visual.FINISHED
Out[2]: True

In [3]: constants.FINISHED is visual.FINISHED
Out[3]: True

Thank you ! i actually didn’t know how to put it in form !
The message error is this one :

File “C:\Program Files (x86)\PsychoPy2\lib\site-packages\pyglet\media\avbin.py”, line 401, in _process_packet
self._buffered_audio_data.append(audio_data)
MemoryError

Thank you Richard for the corrections!

1 Like

I thought I read somewhere on here that avbin is deprecated in psychopy. What version of psychopy are you using?

To start with, yes, I would switch to MovieStim3 (as in the demos) or MoveiStim2. Both should have superior performance to the one you’re using.

Also, just using the demos do those end their scripts and close their windows (trying to work out if this is about your code or something that’s a bigger problem)

Thank you for the advices ! it is yet working :blush: