4

I'm using plyr plugin to play a video on my website.

I'm trying to restart the player (vimeo) when the video ends but I'm not having much luck. I'm even trying to console log a message and that's not even working. Can anyone see where I'm going wrong?

JSfiddle attached here.

if ($('.main-hero__youtube__video').length) {
    console.log("Video player init");
    // Define the controls
    var plyr_options = {
        autoplay: true,
        clickToPlay: true,
        showPosterOnEnd: true,
        controls: ['mute','progress','play']
    };

    // Create the plyr instances - this defines players[0] as our primary player.
    var players =  plyr.setup(plyr_options);

    players[0].on('ended', function(event) {
      console.log("test");
    });

}
beaver
  • 17,333
  • 2
  • 40
  • 66
finners
  • 875
  • 16
  • 31
  • Your fiddle fires the event on `pause`, which it does fine. Actually using the `ended` event (as you have in your edited question) makes the log message appear when the video ends. – Just a student Mar 07 '17 at 10:29
  • Thanks @justastudent. I've just seen it's working in my fiddle. I'm using it within a drupal site and for some reason it's not playing ball which is why I never checked to see if it was working in the jsfiddle. Back to the drawing board to see why it's not working with Drupal. – finners Mar 07 '17 at 10:54

1 Answers1

0

You were close, you just needed to call the restart() method (or play() if you're looking for an infinite loop) on your player instance:

players[0].on('ended', function(event) {
    players[0].restart();
    // players[0].play(); // infinite loop
});

https://jsfiddle.net/yunxyfbh/

GFoley83
  • 3,439
  • 2
  • 33
  • 46
  • Hi @GFoley83, I've just seen it's working in my fiddle (console log at least). I'm using it within a drupal site and for some reason it's not playing ball which is why I never checked to see if it was working in the jsfiddle. Back to the drawing board to see why it's not working with Drupal.Thanks. – finners Mar 07 '17 at 10:57
  • 1
    @FrazerFindlater I'll need more info to be able to help then. Is the code in the jsfiddle the full code? If not please update it, otherwise can you link to the actual page? Also, I can see that you're using jQuery, are you sure that jQuery has loaded and that you're waiting until the DOM ready event e.g. code inside `$(function() { . . . });` before running your code? Are there any errors in the console? Does the video play correctly but the `ended` event just isn't firing? – GFoley83 Mar 07 '17 at 15:02