Stopping Video When Changing Scenes

no worries :wink:

Hi @DBear
I have the same problem in that I’m trying to stop a video when leaving a scene.

I’ve downloaded your script but being new to this, I’m not sure how I open the script and copy it into the editor or integrate it into my hype project.

Would you mind explaining how to do this please?

Kind regards,
Rhys

HI @Rhys

In your hype project, choose the scene you need this to work in and locate the scene inspector (the one that looks like a pile of papers) and in this pane, near the bottom, you will find a list of actions. Click the plus sign next to the “On Scene Unload” and choose “Run Javascript…” then “New Function…” when the new tab pops up just copy and paste the script into the function.

You need to give your video elements a class name of “video” (you can have whatever class name you want as long as you change it in the script) and when a scene “unloads” the script will loop through any elements with that class name and pause and reset the current time.

1 Like

Wow thank you. I had a slight errkk with finding the class thingie-ma-jig, that but all working. Thank you for your help!

Rhys

Hello , Ihave the same problem, but with Youtube video embeded , It’s not working , the sound is still palying all over the web site.
any ideas ?
Thank you !

Let's use this thread @marion

Hi there, I’m actually having problems with the opposite of this thread’s title.

When switching scenes, my inserted video pauses and only resumes when I switch back to the original scene. If I switch to the next scene after the video is finished and then switch back again, the video restarts.

What I would like to achieve is a video that autoplays once and stops on the last frame, regardless of scene switching. I have coded a restart button for restarting the video.

Ive tried it with and without Autoplay checked and also inside symbols and persistent symbols but I always get the same issue. I may be missing something really obvious and apologies if so. Any ideas please? Thanks in advance!

Viewing your project would be useful. If “Autoplay” is not checked it should not play; so something else is going on.

You don’t need to have the entire video just a short 2 sec section will do.

Thanks for replying JimScott!

I’ve recreated the problem in the two smaller Hype docs attached. vid_test.zip (2.3 MB)

One has Autoplay checked and the other doesn’t but uses Javascript to play the video.

The problem isn’t that the video is autoplaying, I actually want that. My problem is that it pauses when switching scenes and resumes when switching back. Also, if you switch scenes after the video is finished, and switch back again, the video restarts.

I just want the video to play once and stop on the last frame, whether you are viewing that scene or not - no pausing or restarting after switching scenes.

Hope I’m making sense, thanks.

Just but a true/false boolean in the code

    if(typeof playVid == "undefined") {
	
	hypeDocument.getElementById('vid').play();
	
	playVid =  true
	}
1 Like

Nice MarkHunte! Thanks, that partially solved it. The video now continues to play when changing scenes but if you watch the video until it finishes then switch scenes and back again, it still restarts (and reverts back to the old pause/unpause behaviour).

I’ve put your code in both my showNextScene and showPreviousScene button functions.

You can probably tell I’m an idiot when it comes to Javascript but I’m going to fiddle with a few things to see if I can fix it. If you have any suggestions in the meantime, please let me know. Cheers!

Not sure what you are doing…:smile:

It should only play once… ever.

You have the vid play function called via the Custom Behaviour from a main timeline action.
This is pretty much the same as onload.

So the code is run each time the scene loads and the main timeline starts.
The extra code checks for a global var. The first time the code runs the Global is not undefined so the if condition is met and the video plays.

After that regardless of playing the video in full or switching scenes the video will not play because the code also defined the Global var right after it started the video. So the if conditon will now never be met.

vid test 2 MHv1.hype.zip (790.1 KB)

Ok, do you mind having a quick look at this doc and letting me know where I went wrong please? Appreciate it!

vid_test3.zip (1.5 MB)

Why are you doing it in next and previous scene ?
Did you look at the example I posted above

Yes I did, it still pauses the vid when changing scenes… at least on my end

Oh, Even though you switch scenes you want the video to continue while you are on the other scene ?

Or do you want the video just to be on the last frame when you go back ?

Yes I would like it to continue regardless of scene change, play to the end and never restart unless someone uses a restart button

And what if they come back while it is still playing…

That’s fine, it just continues to the end. it’s not integral that users see all of the video so if they miss parts of it while switching scenes, that’s ok

Hmm,

I would say put the video into a Persistant Symbol and add to all scenes… but hide it on othere scenes but I think even that may not be straight forward. If I get sometime I will see what I can come up with. I or someone else may have already done this. So it is worth having a didg through the site in the mean time.