1

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

如果该人观看了超过 50% 的视频,但没有完成,则应通过将 expires_in 更新为实际观看视频的时间长度来更新动作实例以反映该人已停止观看: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);
  }
}

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

4

1 回答 1

0

我做了这个作为替代,但我有一些延迟,当我点击暂停视频时,计时器停止但当我按下播放时,计时器已经从 + 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++;
}
}
于 2013-04-17T10:43:30.947 回答