简体   繁体   中英

How to gracefully stop html5 video playback

Is there a way to cleanly stop the html5 video playback ?

There are options like:

Set videoElement.src = "" . This throws an error on the video element, with code = 4.

OR

call videoElement.load(). This sets the readyState = 0 but there is not much documentation around it.

UX Perspective

Looking on Google for the difference between pause and stop gave me various results which I can summrize as:

Pause : Playback is stopped. Hitting play again continues from last position.
Stop : Playback is stopped. Hitting play again continues from beginning

At least from the UX point of view that covers all grounds

Stopping of loading of media

If your real goal is to stop the buffering process from happening as well, then your current approach seems to be entirely correct, running the following code is not triggering any errors for me in any browser I tried.

 var video = document.querySelector("video"); video.play(); setTimeout(function() { video.pause(0); video.setAttribute("src", ""); }, 5000); 
 <video id="video" controls="" preload="none" mediagroup="myVideoGroup" poster="http://media.w3.org/2010/05/sintel/poster.png" style="height:180px"> <source id="mp4" src="http://media.w3.org/2010/05/sintel/trailer.mp4" type="video/mp4"> <source id="webm" src="http://media.w3.org/2010/05/sintel/trailer.webm" type="video/webm"> <source id="ogv" src="http://media.w3.org/2010/05/sintel/trailer.ogv" type="video/ogg"> </video> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM