Last Updated: February 25, 2016
·
3.502K
· darith

video.js disable simultaneous video playback html5 & flash

When you have more than one instance of the video.js html5 video player, use this code to pause a previously running video when clicking play on another video, allowing only one video to run or play at a time. This works for both HTML5 and flash versions of video.js in case you only want to run a single MP4 for both versions. This must launch after everything on the page has loaded (hence the window.onload). This is confirmed and tested to work on IE8-IE11,FF,Chrome,Safari (iOS devices uses native player to play videos, so there's no need for this code on iOS devices):

Code

window.onload = function () {

//html5 - prevent simultaneous video playback - pauses other playing videos upon play
    $('audio,video').bind('play', function() {
            activated = this;
            $('audio,video').each(function() {
                if(this != activated) this.pause();
            });
    });


    //flash - prevent simultaneous video playback - pauses other playing videos upon play
    $(".video-js").click(function(){
            activated = this;
            $('.video-js').each(function() {
                if(this != activated) _V_($(this).attr("id")).pause();
            });
    });

}

Sources

videos.js, HTML5: Jakub Hampl/Stackoverflow

1 Response
Add your response

Where would this code have to be inserted on a Wordpress site. I have 5 js videos on the same page and would like for only one to play at a time. I would really like for the video that is paused to rewind as well. I entered this code in a custom java script area the theme provides but all the videos still play simultaneously.

over 1 year ago ·

Have a fresh tip? Share with Coderwall community!

Post
Post a tip