# Raise RuntimeError(“Cannot close stream, not opened yet.”)

**URL:** <https://discourse.psychopy.org/t/raise-runtimeerror-cannot-close-stream-not-opened-yet/30283>\
**Category:** Builder\
**Tags:** movies\
**Created:** [August 4, 2022, 6:51pm UTC](https://discourse.psychopy.org/t/raise-runtimeerror-cannot-close-stream-not-opened-yet/30283 "2022-08-04T18:51:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ange](https://avatars.discourse-cdn.com/v4/letter/a/76d3ee/32.png) [@Ange](https://discourse.psychopy.org/u/Ange)\
**Post date:** [August 4, 2022, 6:51pm UTC](https://discourse.psychopy.org/t/raise-runtimeerror-cannot-close-stream-not-opened-yet/30283/1 "2022-08-04T18:51:48Z")

</div>

Hello,

This is my overall task:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/1/8/181623d81ed90411ac987e8bb40a36002c9e995c.png)  
In this task, people see a movie for a randomly varied time before responding to a visual analog scale (VAS).  
Details of the task and how it is coded are here: [Breakdown after stimulus random stop - #10 by Ange](https://discourse.psychopy.org/t/breakdown-after-stimulus-random-stop/29867/10). Or right away here the psychopy task:  
[EmpathySTUDY.zip](https://discourse.psychopy.org/uploads/short-url/xZTYspvjyHWtHtRdiryjSV5KROk.zip) (77.7 KB)  
One movie stimuli example is here: [e.mp4 - Google Drive](https://drive.google.com/file/d/1MC6QXG_Q0ODJKpUN5Qt5iqkCEHd05Mv8/view?usp=sharing)

**What specifically went wrong ?:**  
After the movie, the experiment breaks down with the error

> raise RuntimeError(“Cannot close stream, not opened yet.”)

Online I don’t find much about this error… In the VAS routine, I added the following code in the Start section, but it does not help…

 ![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/8/6/86aac10f6bd9e235b932059dea55a04f78dc1d00.png)

Happy for any suggestions… Thanks a lot in advance 🙂

---

<div class="post-metadata">

**Author:** ![cpat3](https://avatars.discourse-cdn.com/v4/letter/c/76d3ee/32.png) [@cpat3](https://discourse.psychopy.org/u/cpat3)\
**Post date:** [August 16, 2022, 8:55pm UTC](https://discourse.psychopy.org/t/raise-runtimeerror-cannot-close-stream-not-opened-yet/30283/2 "2022-08-16T20:55:43Z")

</div>

Was a solution ever found to this? I am having the exact same issue, although changing the duration to a number (like in your previous post) doesn’t work for me.

---

<div class="post-metadata">

**Author:** ![Ange](https://avatars.discourse-cdn.com/v4/letter/a/76d3ee/32.png) [@Ange](https://discourse.psychopy.org/u/Ange)\
**Post date:** [August 16, 2022, 9:11pm UTC](https://discourse.psychopy.org/t/raise-runtimeerror-cannot-close-stream-not-opened-yet/30283/3 "2022-08-16T21:11:20Z")

</div>

Hi!  
No, no solution found so far…

---

<div class="post-metadata">

**Author:** ![TParsons](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/tparsons/32/8301_2.png) [@TParsons](https://discourse.psychopy.org/u/TParsons)\
**Post date:** [August 18, 2022, 10:00am UTC](https://discourse.psychopy.org/t/raise-runtimeerror-cannot-close-stream-not-opened-yet/30283/4 "2022-08-18T10:00:09Z")

</div>

It looks like the call to `movie.stop` is being reached before `movie.start` - what version of PsychoPy are you running in?

---

<div class="post-metadata">

**Author:** ![cpat3](https://avatars.discourse-cdn.com/v4/letter/c/76d3ee/32.png) [@cpat3](https://discourse.psychopy.org/u/cpat3)\
**Post date:** [August 18, 2022, 3:59pm UTC](https://discourse.psychopy.org/t/raise-runtimeerror-cannot-close-stream-not-opened-yet/30283/5 "2022-08-18T15:59:20Z")

</div>

I found a solution in my experiment (thanks to some help from colleagues)! I had to change the duration of the videos in the Excel sheet to be a little longer than the actual video duration. I added two seconds, but you can probably play around with it in your experiment. We think this is necessary because the video takes some time to buffer and so you need to include that in your duration time. Hopefully this helps!

---

<div class="post-metadata">

**Author:** ![Ange](https://avatars.discourse-cdn.com/v4/letter/a/76d3ee/32.png) [@Ange](https://discourse.psychopy.org/u/Ange)\
**Post date:** [August 18, 2022, 6:41pm UTC](https://discourse.psychopy.org/t/raise-runtimeerror-cannot-close-stream-not-opened-yet/30283/6 "2022-08-18T18:41:58Z")

</div>

I tried different versions of the possibilities below:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/c/b/cb58b837599ef8234089bc3e871a86418bc44145.png)

It is not working in any of those I tried…

The thing is that is pays the movie as long as I want it, but it does not pass to the next routine which is a text component with a slider.

---

<div class="post-metadata">

**Author:** ![Ange](https://avatars.discourse-cdn.com/v4/letter/a/76d3ee/32.png) [@Ange](https://discourse.psychopy.org/u/Ange)\
**Post date:** [October 30, 2022, 1:45am UTC](https://discourse.psychopy.org/t/raise-runtimeerror-cannot-close-stream-not-opened-yet/30283/7 "2022-10-30T01:45:49Z")

</div>

I got the answer here:

> [@Randomize duration of a Routine in Builder](https://discourse.psychopy.org/t/randomize-duration-of-a-routine-in-builder/21436/23):
>
> In searching other similar posts I found a thread which suggests that it might be because the movie is buffering. However, I also saw that you’ve seen that thread so here is my suggestion. Remove $stim\_dur from the duration of the movie (make it blank) Add the following code to Each Frame if t \> stim\_dur: continueRoutine = False

The solution is to:

1. Remove $stim\_dur from the duration of the movie (make it blank)
2. Add the following code to Each Frame

```auto
if t > stim_dur:
     continueRoutine = False

```
