该代码位于javascript中,我只是在调整它不用于Chrome,因为Chrome现在不支持它。 95%的时间页面被“刷新”,由于某种原因,当您不期望它被卡住时,它会被卡住。因此,为了保持它的重新加载页面,我使用PHP的Youtube API获得了视频时间,并且没有袖手旁观。诀窍是,如果实际上发生“事件”,则变量名dontdo应该更改为true,因此它不会在该时间之后刷新页面,但是由于某种原因,它不会取消重新加载,JAVASCRIPT VARIABLES错了。
var player;
var nosaukums = "randomvideo";
var dontdo;
<?php
$is_chrome = (stripos($_SERVER['HTTP_USER_AGENT'], 'Chrome') !== false);
if($is_chrome == 0){
?>
function readyYoutube(){
if((typeof YT !== "undefined") && YT && YT.Player){
GoAway();
}else{
setTimeout(readyYoutube, 100);
}
}
function GoAway() {
document.location.href = nosaukums;
}
function onYouTubeIframeAPIReady() { // fired automatically
let player = new YT.Player('youtubeplayer', {
events: {
'onStateChange': onPlayerStateChange,
'onError': onPlayerError
}
});
}
function onPlayerStateChange(event) {
if(typeof event === "undefined"){
GoAway();
}
if (event === "") {
GoAway();
}
console.log(event);
switch (event.data) {
case -1: // Unstarted
return;
case 0: // Ended
GoAway();
// record('video ended');
return;
case 1: // Playing
// record('video playing from '+player.getCurrentTime());
return;
case 2: // Paused
dontdo = true;
// record('video paused at ' + player.getCurrentTime());
return;
case 3: // Buffering
dontdo = true;
return;
case 5: // Video cued
dontdo = true;
return;
default:
GoAway();
return;
case null:
GoAway();
return;
}
}
function onPlayerError(event) {
if(typeof event === "undefined"){
GoAway();
}else{
setTimeout(onPlayerError, 100);
}
console.log("Error", arguments);
}
window.setInterval(function(){
if(typeof dontdo === "undefined") {
GoAway();
}
}, <?php
$apikey = "";
$dur = file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=contentDetails&id=$id&key=$apikey");
$VidDuration =json_decode($dur, true);
foreach ($VidDuration['items'] as $vidTime)
{
$VidDuration2 = $vidTime['contentDetails']['duration'];
$dalas = preg_match_all('/(\d+)/',$VidDuration2,$parts);
if($dalas > 2){
$stundasyt = $parts[0][0];
$minutesyt = $parts[0][1];
$sekundesyt = $parts[0][2];
$stundaspyt = $stundasyt * 3600;
$minutespyt = $minutesyt * 60;
$kopalaiksyt = $stundaspyt + $minutespyt + $sekundesyt;
}
if($dalas < 3){
$minutesyt = $parts[0][0];
$sekundesyt = $parts[0][1];
$minutespyt = $minutesyt * 60;
$kopalaiksyt = $minutespyt + $sekundesyt;
}
$timeoutsytmil = $kopalaiksyt * 1000;
$timeoutsytmil2 = $timeoutsytmil + 120000;
echo $timeoutsytmil2;
} ?>);
<?php
}
?>
有时onYouTubeIframeAPIReady()
仍不会发生,或者onPlayerStateChange()
上的情况得到错误的结果。我不知道。 GoAway是整个代码的精髓,但是当您不那么期望它时,它仍然会卡住。
对于那些不会阅读PHP的人。 (真的吗?)
var player;
var nosaukums = "randomvideo";
var dontdo;
function readyYoutube(){
if((typeof YT !== "undefined") && YT && YT.Player){
GoAway();
}else{
setTimeout(readyYoutube, 100);
}
}
function GoAway() {
document.location.href = nosaukums;
}
function onYouTubeIframeAPIReady() { // fired automatically
let player = new YT.Player('youtubeplayer', {
events: {
'onStateChange': onPlayerStateChange,
'onError': onPlayerError
}
});
}
function onPlayerStateChange(event) {
if(typeof event === "undefined"){
GoAway();
}
if (event === "") {
GoAway();
}
console.log(event);
switch (event.data) {
case -1: // Unstarted
return;
case 0: // Ended
GoAway();
// record('video ended');
return;
case 1: // Playing
// record('video playing from '+player.getCurrentTime());
return;
case 2: // Paused
dontdo = true;
// record('video paused at ' + player.getCurrentTime());
return;
case 3: // Buffering
dontdo = true;
return;
case 5: // Video cued
dontdo = true;
return;
default:
GoAway();
return;
case null:
GoAway();
return;
}
}
function onPlayerError(event) {
if(typeof event === "undefined"){
GoAway();
}else{
setTimeout(onPlayerError, 100);
}
console.log("Error", arguments);
}
window.setInterval(function(){
if(typeof dontdo === "undefined") {
GoAway();
}
}, timeofvideo );