gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 09:13:05 26 4
gpt4 key购买 nike

我搜索了很多方法来获取访问我网站的用户的视频观看秒数。视频页面嵌入了 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com