Can Javascript detect when an embedded Youtube video has ended?

好久不见. 提交于 2019-12-20 05:30:15

问题


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 "play" button. The video is initially set to "display:none".

I've deployed the code below so that when you click on the div, the div disappears and the video now displays and begins to play.

How can I edit the code so that when the video has finished playing, the video disappears and the div re-appears?

<div  class="videodiv" this.style.cursor='pointer'; 
onclick="thevid=document.getElementById('thevideo'); thevid.style.display='block'; 
this.style.display='none'; "></div>
<div  class="youtubevid" id="thevideo" style="display: none;"><object width="420" 
height="160"><param name="movie" value="//www.youtube.com/v/vIdeoUrl?
 hl=en_US&amp;version=3&amp;rel=0&amp;controls=0&amp;showinfo=0&amp;autoplay=1">
</param><param name="allowFullScreen" value="true"></param><param 
name="allowscriptaccess" value="always">
</param><embed src="//www.youtube.com/v/vIdeoUrl?
hl=en_US&amp;version=3&amp;rel=0&amp;controls=0&amp;showinfo=0&amp;autoplay=1" 
type="application/x-shockwave-flash" width="420" height="160" 
allowscriptaccess="always" allowfullscreen="true"></embed></object></div>

回答1:


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




回答2:


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,



来源:https://stackoverflow.com/questions/27073494/can-javascript-detect-when-an-embedded-youtube-video-has-ended

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!