如何使用Google Youtube API在asp.net中播放视频并捕获onStatusChange事件?

时间:2011-02-11 15:28:58

标签: c# .net asp.net youtube

我有两个问题

1.我只想知道如何使用Google Youtube .Net Library在YouTube网站上嵌入youtube视频?

2.当用户点击youtube播放器上的播放按钮时,我希望此操作可以触发一个事件,使我能够向某人发送电子邮件。

如果有人能证明代码和解释,我将非常感谢:)

提前致谢。

温斯顿

1 个答案:

答案 0 :(得分:0)

是我。超过1年后,当我看到这个问题没有得到任何人的回答。 我决定写下我的解决方案,希望它会有所帮助。

  1. 导入Google Youtube播放器
  2.  <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> 
      
    3. 好的,这就是你需要做的一切:)