admin管理员组文章数量:1245684
Sup hackers!
So I've tried and searched for hours and cant seems to find a solution. On my page i made it to where the video automatically plays when the user scrolls to a certain point of the page. It works great, but I find it annoying how the video will loop over and over. I just want the video to play only ONCE, until the user reloads the page or visits a new page and es back. Then the video can play again. Here is what i got!
HTML:
<video id="vid" width="400">
<source src="assets/img/me.mp4" type="video/mp4">
Your browser does not support HTML5 video. Please update your browser.
</video>
JAVASCRIPT:
$(window).scroll(function(){
var myVideo = document.getElementById("vid");
// console.log($(window).scrollTop());
if($(window).scrollTop() > 300 && $(window).scrollTop() < 975){
myVideo.play();
}else{
myVideo.pause();
}
})
nothing to special, pretty stright forward. I understand there is a html5 attribute for loops called "loop", I dont know if this could be my answer or not? iv'e seen in some HTML5 docs for the loop attribute and value 0 seems like what im looking for? but not sure how to go about it?
Value 0 (default): The video will play only once.
Value 1: The video will loop (forever).
Thanks in advance Ladies and Gents! I really do appreciate your time.
Sup hackers!
So I've tried and searched for hours and cant seems to find a solution. On my page i made it to where the video automatically plays when the user scrolls to a certain point of the page. It works great, but I find it annoying how the video will loop over and over. I just want the video to play only ONCE, until the user reloads the page or visits a new page and es back. Then the video can play again. Here is what i got!
HTML:
<video id="vid" width="400">
<source src="assets/img/me.mp4" type="video/mp4">
Your browser does not support HTML5 video. Please update your browser.
</video>
JAVASCRIPT:
$(window).scroll(function(){
var myVideo = document.getElementById("vid");
// console.log($(window).scrollTop());
if($(window).scrollTop() > 300 && $(window).scrollTop() < 975){
myVideo.play();
}else{
myVideo.pause();
}
})
nothing to special, pretty stright forward. I understand there is a html5 attribute for loops called "loop", I dont know if this could be my answer or not? iv'e seen in some HTML5 docs for the loop attribute and value 0 seems like what im looking for? but not sure how to go about it?
Value 0 (default): The video will play only once.
Value 1: The video will loop (forever).
Thanks in advance Ladies and Gents! I really do appreciate your time.
Share Improve this question asked Oct 14, 2015 at 2:41 Michael BarreiroMichael Barreiro 3281 gold badge3 silver badges9 bronze badges3 Answers
Reset to default 5The default value of the loop
attribute is false
, so no it's not your solution.
Your problem is that you will call play()
each time your requirements (scroll position) are fullfilled.
What you need is to check if the video already has been played and only if not, then play the video.
Thanksfully, there is an played
attribute on the video Element that will return a "Timerange indicating all the ranges of the video that have been played."
So you could easily make it like so :
$(window).scroll(function(){
var myVideo = document.getElementById("vid");
if($(window).scrollTop() > 300 && $(window).scrollTop() < 975){
if(myVideo.played.length === 0 )
myVideo.play();
}else{
myVideo.pause();
}
})
But as you noticed it will play the video only once, but won't restart the playing after you paused it, even if the end wasn't reached.
Then a solution for this case is to bind a flag on the onended
event of the video :
// first attach the flag in the onended event
$('#vid').on('ended', function(){this.playedThrough = true;});
$(window).scroll(function(){
var myVideo = document.getElementById("vid");
if($(window).scrollTop() > 300 && $(window).scrollTop() < 975){
// only if we didn't reached the end yet
if(!myVideo.playedThrough)
myVideo.play();
}else{
myVideo.pause();
}
})
2020 update:
You can use
<video id="video-test" autoplay="1" />
For my scenario, loop
was working fine, but I also had onPlay
& onPause
declared. onPlay
works well, but onPause
caused the video to loop endlessly, even though loop
was not declared.
To bat this, I detected video play & finish (as i have the controls removed, so pause is not an option) via addEventListener()
- please see below:
const video = document.querySelector(...)
const videoStartedHandler = () => {
// do stuff...
}
const videoEndedHandler = () => {
// do stuff...
}
video.addEventListener('play', videoStartedHandler, false)
video.addEventListener('ended', videoEndedHandler, false)
本文标签: javascriptHow can i make a html video play one onceuntil the page is reloadedStack Overflow
版权声明:本文标题:javascript - How can i make a html video play one once, until the page is reloaded - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740234235a2246170.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论