Can Javascript detect when an embedded Youtube video has ended?

前端 未结 2 2013
遥遥无期
遥遥无期 2021-01-27 04:25

I have a div containing a video. The background of the div features a fake \"play\" button, which I\'ve designed to use as the play button instead of Youtube\'s standard video

相关标签:
2条回答
  • 2021-01-27 04:41

    Here's what you're looking for:

    http://jsfiddle.net/7Gznb/

    // when video ends
        function onPlayerStateChange(event) {        
            if(event.data === 0) {            
                alert('done');
              //do something here
    
            }
    

    That's the Youtube API by the way

    0 讨论(0)
  • 2021-01-27 04:49

    Take a look in the API, it seems there is events.

    player.addEventListener('onStateChange', function(e) {
       if (e.data === 0) {
         // If state is 0 (ended), do something
       }
    });
    
    • API provided by Google.
    • Events API
    • Embedding via JavaScript

    Also, on the side note:

    It looks like your YouTube video needs to be embed via JavaScript for events. To do so,

    0 讨论(0)
提交回复
热议问题