Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add buffering to my HTML video

I have multiple videos on a page.

<div class="row text-center">
    <video width="320" height="240" controls class="myvideo">
        <source src="http://www.html5videoplayer.net/videos/toystory.mp4" type="video/mp4">
        Your browser does not support the video tag.
    </video>
</div>

<div class="row text-center">
    <video width="320" height="240" controls class="myvideo">
        <source src="http://www.html5videoplayer.net/videos/fantasy.mp4" type="video/mp4">
        Your browser does not support the video tag.
    </video>
</div>

I want to add buffering events to them. Also, when I click video 1, video 2 (if playing) should automatically stop. How can I achieve this? They have a common class. Shall I have to implement ids with them?

like image 368
Ali Zia Avatar asked Nov 09 '22 09:11

Ali Zia


1 Answers

give Ids to your videos:

<video width="320" height="240" controls class="myvideo" id="myVideoOne">
<video width="320" height="240" controls class="myvideo" id="myVideoTwo">

for buffering :

var vid = document.getElementById("myVideoOne");
alert("Start: " + vid.buffered.start(0)
    + " End: " + vid.buffered.end(0));

var vid = document.getElementById("myVideoTwo");
alert("Start: " + vid.buffered.start(0)
    + " End: " + vid.buffered.end(0));

for stoping automatically you can use this inside your videoPlay1 and videoPlay2 functions:

function videoPlay1() {
    var video1 = document.getElementById("myVideoOne");
    var video2 = document.getElementById("myVideotwo");

    if (video1.paused) {
        video1.play();
        video2.pause();
    } else {
        video1.pause();
        video2.pause();
    }
}

function videoPlay2() {
    var video1 = document.getElementById("myVideoOne");
    var video2 = document.getElementById("myVideotwo");

    if (video2.paused) {
        video2.play();
        video1.pause();
    } else {
        video1.pause();
        video2.pause();
    }
}

example : to set a loader for myVideoOne you can use this jquery: css:

video.loading {
    background: black url(/yourGifImg.gif) center center no-repeat;
}

jquery:

$('#myVideoOne').on('loadstart', function (event) {
    $(this).addClass('loading')
});
$('#myVideoOne').on('canplay', function (event) {
    $(this).removeClass('loading')
});
like image 123
Simo Avatar answered Nov 14 '22 22:11

Simo