pausing and playing multiple HTML5 videos using jQuery get(0) indexing?

Go To StackoverFlow.com

3

I have a page of several videos. One can click a thumbnail to play each video. The problem is for that for greater than 2 videos, the clicking on the 3rd thumbnail doesn't pause the 2nd video so I get 2 videos playing at the same time. I'm also using a fadeOut() to toggle the visibility of each video and this works fine regardless of the number of videos. Therefore, I think the issue is with the get(0) part of the code.

here's a jsfiddle that displays the problem: http://jsfiddle.net/trpeters1/EyZdy/28/

Additionally, here's a more verbose block of code that should make the problem clear:

$(function(){
      $('#video_1,#video_2,#video_3,#video_4,#video_5,#video_6').hide();

      $('.icon_1').click(function(){
            $('#video_2,#video_3,#video_4,#video_5,#video_6').fadeOut(function(){
                  $('#video_1').fadeIn();
            $('.video_2,.video_3,.video_4,.video_5,.video_6').get(0).pause();
            $('.video_2,.video_3,.video_4,.video_5,.video_6').get(0).currentTime = 0;
            $('.video_1').get(0).play();
            });
      });


      $('.icon_2').click(function(){
            $('#video_1,#video_3,#video_4,#video_5,#video_6').fadeOut(function(){
                  $('#video_2').fadeIn();
            $('.video_1,.video_2,.video_4,.video_5,.video_6').get(0).pause();
            $('.video_1,.video_2,.video_4,.video_5,.video_6').get(0).currentTime = 0;
            $('.video_2').get(0).play();
            });
      });

      $('.icon_3').click(function(){
            $('#video_1,#video_2,#video_4,#video_5,#video_6').fadeOut(function(){
                  $('#video_3').fadeIn();
            $('.video_1,.video_2,.video_4,.video_5,.video_6').get(0).pause();
            $('.video_1,.video_2,.video_4,.video_5,.video_6').get(0).currentTime = 0;
            $('.video_3').get(0).play();
            });
      });
});

and the html:

<div id="video_1">
<div id="mediaplayer">cadillac</div>
    <video class="video_1" width="100%" height="100%" controls="controls">
        <source src="videos/cadillac_x264.mp4" type="video/mp4" />
    <object data="videos/cadillac_x264.mp4" width="100%" height="100%">
</object> 
</video>
</div>

<div id="video_2">
<div id="mediaplayer2">nike</div>
    <video class="video_2" width="100%" height="100%" controls="controls">
    <source src="videos/Nike_Pretty - Computer_x264.mp4" type="video/mp4" />
<object data="videos/Nike_Pretty - Computer_x264.mp4" width="100%" height="100%">
</object> 
</video>
</div>

<div id="video_3">
<div id="mediaplayer3">russian standard</div>
    <video class="video_3" width="100%" height="100%" controls="controls">
    <source src="videos/Russian_Standard.mp4" type="video/mp4" />
<object data="videos/Russian_Standard.mp4" width="100%" height="100%">
</object> 
</video>
</div>
2012-04-04 21:16
by Adam


11

When you do the following:

$('.video_2,.video_3,.video_4,.video_5,.video_6').get(0)

the "get(0)" returns the first element that matches the selector - in this case, just the first element that matches ".video_2". The rest of the videos are ignored. To do an action on all of the selected elements, check out jQuery's "each()" method. Also, you can simplify your code down to a more generic approach by doing something like this:

<a href="#" class="video-thumbnail" data-video-id="video-1">Video 1</a>
<a href="#" class="video-thumbnail" data-video-id="video-2">Video 2</a>

<video id="video-1"> ... </video>
<video id="video-2"> ... </video>

And then hooking up JS by doing something like this:

$('.video-thumbnail').on('click', function () {
    // Just go ahead and pause/reset all the video elements
    $('video').each(function () {
        this.pause();
        this.currentTime = 0;
    });

    $('#' + $(this).data('video-id')).get(0).play();
});

I've just typed this from my head, but hopefully it will put you in the right direction.

2012-04-04 21:37
by Scott Hamper
thanks P1aincloth3sM4n, i followed what you said about reseting all videos, here's a working solution... http://jsfiddle.net/trpeters1/EyZdy/52 - tim peterson 2012-04-04 21:45
You too, P1aincloth3sM4n, are a champion.. - Adam 2012-04-04 21:51
Works like a charm. Excellent work. Saved my bacon. I do love my bacon - Adam 2012-04-05 12:04
@timpeterson Link is dead. I know its old, but still its dead - Sandy 2016-09-08 16:19


3

thanks P1aincloth3sM4n, i followed what you said about reseting all videos and making a more generalizable solution, for those interested please see the following working jsfiddle: http://jsfiddle.net/trpeters1/EyZdy/52

2012-04-04 21:46
by tim peterson
Glad I could help you out! Would you mind marking my answer as the accepted solution so I get some rep? : - Scott Hamper 2012-04-04 21:53
i just upvoted it, i think Adam, since he wrote the question, is the only one who can mark it as the accepted solutio - tim peterson 2012-04-04 22:02
Upvoted and chosen - Adam 2012-04-05 12:04
@timpeterson Link is dead. I know its old, but still its dead - Sandy 2016-09-08 16:19
Replace trpeters1 with timrpeterson and it should wor - tim peterson 2016-09-08 22:49
the working link : http://jsfiddle.net/timrpeterson/EyZdy/52 - Rachel Gallen 2017-02-28 12:15


0

Easy solution to play only one HTML5 video element on page using JQUERY:

$(function() {
$("video").each(function() {
    this.pauseOthers = function(event) {
        $('video').addClass('stopvideo');
        $(this).removeClass('stopvideo');
        $('.stopvideo').each(function() {
            this.pause();
        });
    };
    this.addEventListener("play", this.pauseOthers.bind(this), false);
});
});
2015-10-30 05:34
by eudard
Ads