Make callback after play button is pushed - Youtub

2019-02-10 06:10发布

Is it possible to do a javascript action after play button is pushed? I know I will need to use onStateChange function form Youtube's API. But I really don't know where to start? Any help? Thank you.

--

I have also found something here: http://apiblog.youtube.com/2011/01/introducing-javascript-player-api-for.html

4条回答
smile是对你的礼貌
2楼-- · 2019-02-10 06:34
    <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

查看更多
淡お忘
3楼-- · 2019-02-10 06:41

Hey here is the answer . http://jsfiddle.net/masiha/4mEDR/ Enjoy ...let me know if you have more qtns

查看更多
可以哭但决不认输i
4楼-- · 2019-02-10 06:45

Here's a solution that you should be able to extend easily: http://jsbin.com/evagof

查看更多
倾城 Initia
5楼-- · 2019-02-10 06:47

Maybe Youtube made some changes since this question was posted, but most of the answers didn't work. I found that the easiest way was to use the iframe_api (https://developers.google.com/youtube/iframe_api_reference)

Here is an example

https://thimble.webmaker.org/en-US/project/39354/edit

查看更多
登录 后发表回答