Stopping Video When Changing Scenes

How do I stop a video when changing scenes so that when I return to a scene the video starts from the beginning?

Thanks!

this can be useful Can't remove iframe by class rather than ID
made by @rene

var i
var vElements = document.getElementsByClassName("video")
for (i = 0; i < vElements.length; i++) {vElements[i]. innerHTML = '';}

It’s not for an embedded video, it is for a video added directly into hype

Here is an example. Call a Javascript function on Scene unload that pauses the video and resets it’s time. (must give the video element an ID)

stopVideoWhenExitingScene.hype.zip (799.0 KB)

Thanks, @DBear. This little script just saved my booty. Most appreciated.

Hi @DBear, any chance one could run that script for multiple videos within the same website? Maybe by using a Class Name instead of unique ID?

Thanks!
Thiago

Hi @thiago.valenti

A straight forward way is to give every video a class name and then loop through them and stop them all.

var videos = document.querySelectorAll('.video');
	
if (videos.length != 0){
	for (var i=0; i < videos.length; i++){
		videos[i].pause();
		videos[i].currentTime = 0;
	}
}

I’m not sure this is what you want as a bit more information may be needed.

Thanks @DBear, doesn’t seem to be working on desktop, but it’s fine on the iPad.
Maybe it’s making no difference and the iPad is running from the start as default. Any ideas what it might be? A classed all the videos as “video”.

Thanks again.

How have you implemented this? and what are you trying to achieve exactly? maybe you could share your document.

The above code should be placed into a function that is run on scene unload in whichever scene you need to stop and “rewind” the videos before moving onto the next.

There should be no apparent reason why it would not work on desktop. :wink:


Like these attached, can’t upload the file because it’s for a corporate client and something that hasn’t been launched yet =/

Am I missing anything?

I’ve just tried cross browser, and it seems to work on Safari but not Chrome.
Hence why it was working on the iPad.

When running a copy here I get no problems.

stopVideoWhenExitingScene-v2.zip (1.0 MB)

I must have copied something wrong on the javascript then, all working here now.
Thanks a lot! =)

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.