【问题标题】:how to play video in asp.net with Google Youtube API and capture the onStatusChange event?如何使用 Google Youtube API 在 asp.net 中播放视频并捕获 onStatusChange 事件?
【发布时间】:2011-02-11 15:28:57
【问题描述】:

我有 2 个问题

1.我只是想知道如何使用 Google Youtube .Net Library 将 youtube 视频嵌入到 asp.net 网站?

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

如果有人可以演示代码和解释,我将不胜感激:)

提前致谢。

温斯顿

【问题讨论】:

  • 我已经使用 javascript Youtube api 引入了类似的东西。 link .. 如果您还需要什么,请告诉我...
  • @manish,谢谢:)虽然我已经完成了,但非常感谢人^^

标签: c# .net asp.net youtube


【解决方案1】:

是我。 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> 
    

好的,这就是你需要做的一切:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-03
    • 1970-01-01
    • 2014-04-11
    • 2012-04-09
    • 2018-04-02
    • 2012-12-03
    • 1970-01-01
    相关资源
    最近更新 更多