2012-05-14 64 views

回答

7

該處理啓動和停止的事件爲例:

HTML文件(index.html的):

<!DOCTYPE html> 
<html> 
    <head> 
     <title>Stackoverflow</title> 
     <script type="text/javascript" src="http://www.youtube.com/player_api"> </script> 
     <script type="text/javascript" src="sof.js"> </script> 
    </head> 
    <body> 
     <div id="player"></div> 
    </body> 
</html> 

和JavaScript(sof.js):

var player; 
// This function creates an <iframe> (and YouTube player) 
// after the API code downloads. 
function onYouTubePlayerAPIReady() { 
    player = new YT.Player('player', { 
     height: '390', 
     width: '640', 
     videoId: 'u1zgFlCw8Aw', 
     events: { 
      'onStateChange': function (event) { 
       switch (event.data) { 
        case -1: 
         console.log ('unstarted'); 
         break; 
        case 0: 
         console.log ('ended'); 
         break; 
        case 1: 
         console.log ('playing'); 
         break; 
        case 2: 
         console.log ('paused'); 
         break; 
        case 3: 
         console.log ('buffering'); 
         break; 
        case 5: 
         console.log ('video cued'); 
         break; 
       } 
      } 
     } 
    }); 
} 

對於每個案例你可以設置一個處理程序。

如需進一步信息:

  1. YT Player Getting Started
  2. YT Javascript API Events
+0

卓越的,這正是我所需要的 –

+1

你是一個很好的Wik。對此帖子+1。 –

+0

我寫了一篇博客文章,詳細介紹了一個類似的YouTube示例,但採取了一些不同的策略:http://www.objectpartners.com/2013/08/21/triggering-a-youtube-video-from-clicking-a-按鈕圖像和 - 更換 - 當端/ –

0

唯一使用的事件是:

1 - onStateChange 
2 - onPlaybackQualityChange 
3 - onError 
4 - onApiChange 

- 活動處理程序:

1- onYouTubePlayerReady(playerid)