2011-11-16 27 views
5

Có thể thực hiện hành động javascript sau khi nút phát được đẩy không? Tôi biết mình sẽ cần sử dụng API của API onStateChange. Nhưng tôi thực sự không biết bắt đầu từ đâu? Bất kỳ giúp đỡ? Cảm ơn bạn.Gọi lại sau khi nút phát được đẩy - Video nhúng Youtube

-

Tôi cũng đã tìm thấy một cái gì đó ở đây: http://apiblog.youtube.com/2011/01/introducing-javascript-player-api-for.html

Trả lời

3
<script src="http://www.google.com/jsapi" type="text/javascript"></script> 
    <script type="text/javascript"> 
     google.load("swfobject", "2.1"); 
    </script>  
    <script type="text/javascript"> 
     // Update a particular HTML element with a new value 
     function updateHTML(elmId, value) { 
     document.getElementById(elmId).innerHTML = value; 
     } 


     // This function is called when the player changes state 
     function onPlayerStateChange(newState) { 
     updateHTML("playerState", newState); 
     if(newState === 1){ 
      //if the player is now playing 
      //add your code here 
     } 
     } 

     // This function is automatically called by the player once it loads 
     function onYouTubePlayerReady(playerId) { 
     ytplayer = document.getElementById("ytPlayer"); 
     // This causes the updatePlayerInfo function to be called every 250ms to 
     // get fresh data from the player 
     setInterval(updatePlayerInfo, 250); 
     updatePlayerInfo(); 
     ytplayer.addEventListener("onPlayerStateChange"); 
     } 

     // The "main method" of this sample. Called when someone clicks "Run". 
     function loadPlayer() { 
     // The video to load 
     var videoID = "ylLzyHk54Z0" 
     // Lets Flash from another domain call JavaScript 
     var params = { allowScriptAccess: "always" }; 
     // The element id of the Flash embed 
     var atts = { id: "ytPlayer" }; 
     // All of the magic handled by SWFObject (http://code.google.com/p/swfobject/) 
     swfobject.embedSWF("http://www.youtube.com/v/" + videoID + 
          "?version=3&enablejsapi=1&playerapiid=player1", 
          "videoDiv", "480", "295", "9", null, null, params, atts); 
     } 
     function _run() { 
     loadPlayer(); 
     } 
     google.setOnLoadCallback(_run); 
    </script> 
<div id="videoDiv">Loading...</div> 
<p>Player state: <span id="playerState">--</span></p> 

http://code.google.com/apis/ajax/playground/?exp=youtube#polling_the_player

4

Hey ở đây là câu trả lời . http://jsfiddle.net/masiha/4mEDR/ Thưởng thức ... cho tôi biết nếu bạn có nhiều qtns

+0

này là tốt hơn sau đó kịch bản khác liên quan đến: D – aslamdoctor

+0

Awesome, tôi đã mong đợi một cái gì đó đau đớn, nhưng bạn giải pháp là siêu dễ dàng. –

+0

Cảm ơn bạn, tôi đã cố gắng hết mình! – Sumas

Các vấn đề liên quan