如何在youtube中嵌入搜索事件嵌入api

时间:2015-03-27 05:27:40

标签: youtube-api

您好我正在使用youtube iframe嵌入API。我希望用户跟踪搜索视频事件。请帮助我如何跟踪这个。

2 个答案:

答案 0 :(得分:12)

单独使用api跟踪事件没有简单的方法。

您可以在间隔中运行javascript函数,并检查测量的时差是否与预期的不同

以下是一个示例代码:

<html>
    <body>
        <div id="player"></div>
        <script>
            var tag = document.createElement('script');
            tag.src = "https://www.youtube.com/iframe_api";

            var firstScriptTag = document.getElementsByTagName('script')[0];
            firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

            var player;

            function onYouTubeIframeAPIReady() {
                console.log("ready");
                player = new YT.Player('player', {
                    height: '390',
                    width: '640',
                    videoId: 'cRmNPE0HwE8',
                    events: {
                        'onReady': onPlayerReady,
                            'onStateChange': onPlayerStateChange
                    }
                });
                //console.log(player);
            }

            function onPlayerReady(event) {
                event.target.playVideo();

                /// Time tracking starting here

                var lastTime = -1;
                var interval = 1000;

                var checkPlayerTime = function () {
                    if (lastTime != -1) {
                        if(player.getPlayerState() == YT.PlayerState.PLAYING ) {
                            var t = player.getCurrentTime();

                            //console.log(Math.abs(t - lastTime -1));

                            ///expecting 1 second interval , with 500 ms margin
                            if (Math.abs(t - lastTime - 1) > 0.5) {
                                // there was a seek occuring
                                console.log("seek"); /// fire your event here !
                            }
                        }
                    }
                    lastTime = player.getCurrentTime();
                    setTimeout(checkPlayerTime, interval); /// repeat function call in 1 second
                }
                setTimeout(checkPlayerTime, interval); /// initial call delayed 
            }
            function onPlayerStateChange(event) {

            }
        </script>
    </body>
</html>

答案 1 :(得分:0)

您可以简单地做到这一点(无需计时器):

<script>
   const tag = document.createElement('script');
   tag.src = "https://www.youtube.com/iframe_api";
   const firstScriptTag = document.getElementsByTagName('script')[0];
   firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

   let previousAction;
   let previousTime;

   function onYouTubeIframeAPIReady() {
      player = new YT.Player('player', {
        events: {
          'onStateChange': onPlayerStateChange
        }
      });
   }

   function onPlayerStateChange({target, data}) {
     if(data != 3) { // we don't need to detect buffering
       return;
     }

     const currentTime = target.getCurrentTime();
     // detect existing action and previous action which is not paused
     if (!previousAction || previousAction != 2 ) {
        // return existing youtube api data (-1 || 0 || 1 || 5 )
        return data;
     } else if (Math.abs(previousTime - currentTime) > 1 ) {
        // we have Seek event and we have time when it was started(previousTime). Also we have finish time of Seek event(currentTime).
        return 'Seek'
     }

     previousTime = currentTime;
     previousAction = data
   }
</script>