Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
176 views
in Technique[技术] by (71.8m points)

jquery - how to play multiple mediaelementplayer one by one in javascript

I have three videos shown on the website:

<video id="player0" class="video-player" muted preload="metadata"...>....</video>

<video id="player1" class="video-player" muted preload="metadata"...>....</video>

<video id="player2" class="video-player" muted preload="metadata"...>....</video>

and in my javascript, I have:

$(document).ready(function() {
    $('.video-player').mediaelementplayer({ 
        alwaysShowControls:true,
        videoVolume: 'vertical',            
        features: ['playpause','current','progress','duration','tracks','volume','fullscreen','mobileautomute'],        
        success: function (mediaElement, domObject) {
            var target = document.body.querySelectorAll('.video-player'); 
            for (a=0;a<target.length;a++){ 
                target[a].style.visibility = 'visible'; 
            } 
            mediaElement.addEventListener('loadedmetadata', function() {
                mediaElement.play();
            }, false);              
        }
    });
});

With this code, all three videos load together and it random pick one video to autoplay, and cannot go to another one to play automatically.

How can I make all three videos all loaded at first time, but play one by one in video 0 to video 1 then video 2?

Thanks.

question from:https://stackoverflow.com/questions/65897598/how-to-play-multiple-mediaelementplayer-one-by-one-in-javascript

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I figure it how to fix my problem:

  1. changed all the preload form metadata to none, as below
<video id="player0" class="video-player" muted preload="metadata"...>....</video>

to

<video id="player0" class="video-player" muted preload="none"...>....</video>
  1. Revised the javascript to below:
$(document).ready(function() {          
    $('.video-player').mediaelementplayer ({ 
        pauseOtherPlayers: true,            
        alwaysShowControls:true, 
        videoVolume: 'vertical', 
        autoplay: true, 
        features: ['playpause','current','progress','duration','tracks','volume','fullscreen','mobileautomute'], 
        success: function (mediaElement, domObject) {               
            var target = document.body.querySelectorAll('.video-player'); 
            for (a=0;a<target.length;a++){ 
                target[a].style.visibility = 'visible'; 
            }
            var theID = mediaElement['attributes']['id'].value;             
            if (theID == "player0"){
                mediaElement.play();
                mediaElement.addEventListener('ended', function() {
                    var videoElem = document.getElementById("player1");                     
                    videoElem.play();                       
                });
            } else if (theID == "player1"){
                mediaElement.addEventListener('ended', function() {
                    var videoElem = document.getElementById("player2");                     
                    videoElem.play();                       
                });
            }               
        } 
    }); 
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...