admin管理员组文章数量:1191729
I have a html page. I have used a video tag for playing video online I have used two video tags, But when I play both videos, then both videos play at same time
I want a solution that, if I play one video and then click on the second one then the first one should be paused and the second one starts playing. Any help would be appreciated.
I have a html page. I have used a video tag for playing video online I have used two video tags, But when I play both videos, then both videos play at same time
I want a solution that, if I play one video and then click on the second one then the first one should be paused and the second one starts playing. Any help would be appreciated.
Share Improve this question edited Jan 16, 2015 at 13:34 Danny Staple 7,3124 gold badges45 silver badges57 bronze badges asked Jan 16, 2015 at 12:47 user3783962user3783962 631 gold badge1 silver badge4 bronze badges 1 |5 Answers
Reset to default 17Vanilla js here.
var videos = document.querySelectorAll('video');
for(var i=0; i<videos.length; i++)
videos[i].addEventListener('play', function(){pauseAll(this)}, true);
function pauseAll(elem){
for(var i=0; i<videos.length; i++){
//Is this the one we want to play?
if(videos[i] == elem) continue;
//Have we already played it && is it already paused?
if(videos[i].played.length > 0 && !videos[i].paused){
// Then pause it now
videos[i].pause();
}
}
}
<video height="169" width="300" preload="none" controls="controls">
<source type="video/webm" src="http://video.webmfiles.org/big-buck-bunny_trailer.webm"></source>
<source type="video/mp4" src="http://video.webmfiles.org/big-buck-bunny_trailer.mp4"></source>
</video>
<video height="169" width="300" preload="none" controls="controls">
<source type="video/webm" src="http://video.webmfiles.org/elephants-dream.webm"></source>
<source type="video/mp4" src="https://archive.org/download/ElephantsDream/ed_hd.mp4"></source>
</video>
One simple solution will be using javascript. Apply play binding to the video tag. In that pause all the other video
$('video').bind('play', function (e)
{
var video = $('video');
for(var i=0;i<video.length;i++)
{
if(video[i] != e.target)
{
video[i].pause();
}
}
});
Not sure but hope it will work for you
// AUTO STOP WHEN CURRENT VIDEO IS PLAYING..
//JAVASCRIPT
document.addEventListener('play', function(e){
var videos = document.getElementsByTagName('video');
for(var i = 0, len = videos.length; i < len;i++){
if(videos[i] != e.target){
videos[i].pause();
}
}
}, true);
///REACT
// componentDidMount() {
// document.addEventListener('play', function(e){
// var videos = document.getElementsByTagName('video');
// for(var i = 0, len = videos.length; i < len;i++){
// if(videos[i] != e.target){
// videos[i].pause();
// }
// }
// }, true);
// }
<video height="169" width="300" preload="none" controls="controls">
<source type="video/webm" src="https://www.w3schools.com/html/mov_bbb.mp4"></source>
</video>
<video height="169" width="300" preload="none" controls="controls">
<source type="video/webm" src="https://archive.org/download/ElephantsDream/ed_hd.mp4"></source>
</video>
you need to monitor the video's "play"
event, and when it fires you stop the other video. that way, when you play one video in whatever way, the other one will stop:
$(function(){
$("#video1").on("play",function(){
$("#video2").trigger("pause");
});
$("#video2").on("play",function(){
$("#video1").trigger("pause");
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<video id="video1" width="300" controls="" onplay="Vid1()">
<source src="http://www.w3schools.com/html/mov_bbb.mp4" type="video/mp4">
<source src="http://www.w3schools.com/html/mov_bbb.ogg" type="video/ogg">
Your browser does not support HTML5 video.
</video>
<video id="video2" width="300" controls=""><br>
<source src="http://techslides.com/demos/sample-videos/small.webm" type="video/webm"><br>
<source src="http://techslides.com/demos/sample-videos/small.ogv" type="video/ogg"><br>
<source src="http://techslides.com/demos/sample-videos/small.mp4" type="video/mp4"><br>
<source src="http://techslides.com/demos/sample-videos/small.3gp" type="video/3gp"><br>
</video>
Add
onplay="stopOtherMedia(this)"
on all audio and video tags
function stopOtherMedia(element) {
$("audio").not(element).each(function(index, audio) {
audio.pause();
});
$("video").not(element).each(function(index, video) {
video.pause();
});
}
本文标签: javascriptPlay one video at a time on html pageStack Overflow
版权声明:本文标题:javascript - Play one video at a time on html page - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738410524a2085297.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
when i click on both video
explain this. – Manwal Commented Jan 16, 2015 at 12:48