Сделать обратный звонок после нажатия кнопки воспроизведения - YouTube для вставки видео - PullRequest
5 голосов
/ 16 ноября 2011

Можно ли выполнить действие javascript после нажатия кнопки воспроизведения?Я знаю, что мне нужно будет использовать функцию onStateChange из API Youtube.Но я действительно не знаю, с чего начать?Любая помощь?Спасибо.

-

Я также нашел здесь кое-что: http://apiblog.youtube.com/2011/01/introducing-javascript-player-api-for.html

Ответы [ 4 ]

7 голосов
/ 16 ноября 2011

Вот решение, которое вы можете легко расширить: http://jsbin.com/evagof

4 голосов
/ 28 сентября 2012

Привет, вот ответ. http://jsfiddle.net/masiha/4mEDR/ Наслаждайтесь ... дайте мне знать, если у вас есть больше Qtns

3 голосов
/ 16 ноября 2011
    <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

2 голосов
/ 28 февраля 2014

Может быть, Youtube внес некоторые изменения, так как этот вопрос был опубликован, но большинство ответов не сработало.Я обнаружил, что проще всего было использовать iframe_api (https://developers.google.com/youtube/iframe_api_reference)

Вот пример

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

...