php - 捕获YouTube播放器的开始按钮和观看的秒数

标签 php javascript ajax jquery youtube

我搜索了很多方法来获取访问我网站的用户的视频观看秒数。视频页面嵌入了 YouTube 视频,我需要在 Facebook 上发布的观看秒数。规则说:

If the person watches more than 50% of a video, but does not complete it, the action instance should be updated to reflect that the person has stopped watching by updating expires_in to the length of time the video was actually watched from: http://developers.facebook.com/docs/opengraph/guides/video.watches/

到目前为止,我临时编写了以下代码。当用户按下开始视频时,我启动一个计数器。问题在代码注释中。

function stopCycle(event) {
    switch( event.data ) {
//event.data can be 1 or 2 . If user press play is event.data = 1 and if press
pause event.data = 2 and again if press play will be case 1. So each time
when user press START - PAUSE - CONTINUE - PAUSE event.data will have the value
        case 1:
// here i post on facebook after 10 seconds as rules said
            setTimeout('postwatch()', 10000);


//i get with ajax from youtube the video time (in seconds)
            $.ajax({
            url: "http://gdata.youtube.com/feeds/api/videos/<?php echo get_post_meta(get_the_ID(), 'tj_video_id_url', TRUE); ?>?alt=json",
            dataType: "jsonp",
            success: function (data) {
//here I appeal the ontimer function but the bug is that every tine i press play button even and after pause get appeal and the counter got crazy
                onTimer(data.entry.media$group.yt$duration.seconds);
            }
            });

        break;
        }
            console.log("onStateChange has fired!\nNew state:" );

}

i = 0;
function onTimer(videotime) {
  document.getElementById('mycounter').innerHTML = i;
  i++;
  if (i > videotime) {
    alert('Finish!');
  }
  else {
    setTimeout(onTimer, 1000);
  }
}

您知道任何替代方案或可以回答我正确的方法吗?

最佳答案

我将其作为替代方案,但我有一些延迟,当我单击“暂停视频”时,计时器停止,但是当我按下播放时,计时器已经从 + 1 秒开始,但在视频中我可能会在 4,5 秒和我的计时器上暂停从 5 开始:

    var started = 0;
    function stopCycle(event) {
        switch( event.data ) {
            case 1:
            ispaused = false;
            if(started == 0) {
                setTimeout('postwatch()', 10000);
                onTimer(youtube.youtubeinfo.entry.media$group.yt$duration.seconds, 0);
                started = 1;
            }else if(started == 1) {
                onTimer(youtube.youtubeinfo.entry.media$group.yt$duration.seconds, i);
            }

            break;
            case 2:
                setPause();
                console.log('pauz');
            break;
            }
    }

var ispaused=false;
function setPause(){
   ispaused=!ispaused;
}

i = 0;
function onTimer(videotime, remaining) {
if(!ispaused) {
  document.getElementById('mycounter').innerHTML = i;
  if(remaining > 0) {
        i = remaining;
        console.log('ramas');
    }

  console.log(i);
  if (i > videotime) {
    alert('Finish!');
  }else{
    setTimeout(onTimer, 1000);
  }
    i++;
}
}

关于php - 捕获YouTube播放器的开始按钮和观看的秒数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16045013/

相关文章:

ajax - HTML5 文件 API 和 AJAX 上传分块问题

php - 匹配初始化的整数值

php - 如果用户不是来自 HERE,则不处理 - 如何处理?

javascript - 使用 Bootstrap 按钮验证错误

php - 如果没有 ajax readystate() 中的 alert(),循环将无法工作

javascript - 来自多个来源的 Ajax 请求,包括 google api

php - 具有相同请求的 Jquery POST 不起作用

php - 表格未选取名字

javascript - 循环遍历单个 <td> 元素 - Laravel

javascript - javascript 模板字符串中的默认值