2011-02-11 69 views
0

我有2個問題如何使用Google Youtube API在asp.net中播放視頻並捕獲onStatusChange事件?

1.我只是想知道如何使用Google Youtube .Net Library在asp.net網站中嵌入youtube視頻嗎?

2.當用戶點擊YouTube播放器上的播放按鈕時,我希望此操作可以觸發一個事件,使我可以向某人發送電子郵件。

如果有人能證明代碼和交代,我會高度讚賞事先知道:)

感謝。

Winston

+0

我已經inplimented使用JavaScript類似的東西Youtube API。 [鏈接](http://www.manishojha.com)..讓我知道如果你需要別的什麼... – manish 2011-02-16 07:47:47

回答

0

這是我。超過1年後,當我看到這個問題沒有被任何人回答。 我決定寫下我的解決方案,希望這會有所幫助。

  1. 導入谷歌YouTube播放器
<script src="http://www.google.com/jsapi" type="text/javascript"></script> 
  1. 添加js代碼

    google.load("swfobject", "2.1"); 
    
        function updateHTML(elmId, value) { 
         document.getElementById(elmId).innerHTML = value; 
        } 
    
        // Loads the selected video into the player. 
        function loadVideo() { 
         var selectBox = document.getElementById("videoSelection"); 
         var videoID = selectBox.options[selectBox.selectedIndex].value 
    
         if (ytplayer) { 
          ytplayer.cueVideoById(videoID); 
    
         } 
        } 
    
        // This function is called when an error is thrown by the player 
        function onPlayerError(errorCode) { 
         alert("An error occured of type:" + errorCode); 
        } 
    
        var target = ""; 
    
        // This function is automatically called by the player once it loads 
        function onYouTubePlayerReady(playerId) { 
    
         ytplayer = document.getElementById("ytPlayer"); 
         ytplayer.addEventListener("onError", "onPlayerError"); 
         ytplayer.addEventListener("onStateChange","onPlayStateChange"); 
    
         target = document.forms[0].target; 
    
        } 
    
    
    
        //unstarted (-1), ended (0), playing (1), paused (2), buffering (3), video cued (5) 
        var n = 0; 
        function onPlayStateChange(newState) { 
    
         if (newState == 1) { 
    
          document.forms[0].elements["Send"].value = "1"; 
          document.forms[0].target = "SendFrame"; 
          document.forms[0].submit(); 
          updateHTML("counter", ++n); 
         } 
         else { 
          document.forms[0].elements["Send"].value = "0"; 
          document.forms[0].target = target; 
         }   
        } 
    
        // The "main method" . 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 + 
             "&enablejsapi=1&playerapiid=player1", 
             "videoDiv", "480", "295", "8", null, null, params, atts); 
        } 
        function _run() { 
         loadPlayer(); 
        } 
        google.setOnLoadCallback(_run); 
    
    
    </script> 
    
  2. 添加HTML代碼2個div元素,一個是球員,一個計數器

    <div id="videoDiv">Load...</div>"  
    <div id="counter">0</div> 
    

好了,這就是你需要做的:)

相關問題