Stopping Video When Changing Scenes

video

(Sam) #1

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

Thanks!


Website loading
(Freelancer) #2

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 = '';}

(Sam) #3

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


#4

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)


(Jeff) #5

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


(Thiago Valenti) #6

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


#7

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.


(Thiago Valenti) #8

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.


#9

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:


(Thiago Valenti) #10


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.


#11

When running a copy here I get no problems.

stopVideoWhenExitingScene-v2.zip (1.0 MB)


(Thiago Valenti) #12

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


#13

no worries :wink:


(Rhys Beetham) #14

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


#15

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.


(Rhys Beetham) #16

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


#17

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 !


#19

Let’s use this thread @marion


#20

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!


#21

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.