使用YouTube API设置视频开始时间 - “开始”按钮playerVars选项用于工作,但现在不行? [英] Setting video start time with the YouTube API - "start" playerVars option used to work, but now does not?
问题描述
我使用YouTube API写了这个愚蠢的玩具。基本上它只是在不同的随机时间开始多个视频副本。这一年工作得很好。但今天我注意到随机启动时间不再有效,它只是在t = 0时启动所有视频(在多个浏览器/操作系统/计算机上测试)。
I wrote this stupid toy using the YouTube API. Basically it just starts multiple copies of a video at different randomized times. That worked great for like a year. But today I noticed that the randomized start times no longer work, it just starts all of the videos at t=0 (tested on multiple browsers/OSes/computers).
知道什么是错的吗?我只是将一个随机数传递给 start
参数。看似简单。
Any idea what's wrong? I'm just passing a random number to the start
parameter. Seems simple.
这是我的代码。关键部分在 add
函数中,其中 start
参数设置在 playerVars
。
Here's my code. The key part is in the add
function, where the start
parameter is set in playerVars
.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Flockas</title>
</head>
<body>
<button id="pause">Pause Flockas</button>
<button id="add">Add One Flocka</button>
<button id="remove">Remove One Flocka</button>
<p>
<div id="flockas"></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script>
var flockas = [];
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
function onPlayerReady(event) {
event.target.playVideo();
}
function add() {
var start = Math.random() * 60;
$("#flockas").append('<div id="flocka' + flockas.length + '" class="flocka"></div>');
flockas.push(new YT.Player('flocka' + flockas.length, {
height: '105',
width: '140',
videoId: 'yOc-MXGuKgs',
playerVars: {
autoplay: 1,
loop: 1,
playlist: 'yOc-MXGuKgs',
start: start
},
events: {
'onReady': onPlayerReady
}
}));
}
function remove() {
var divs = $(".flocka");
$(divs[divs.length - 1]).remove();
flockas.splice(flockas.length - 1)
}
function pause() {
var pauseEl = $("#pause");
if (pauseEl.html() === "Pause Flockas") {
for (var i = 0; i < flockas.length; i++) {
flockas[i].pauseVideo();
}
pauseEl.html("Play Flockas");
} else {
for (var i = 0; i < flockas.length; i++) {
flockas[i].playVideo();
}
pauseEl.html("Pause Flockas");
}
}
function onYouTubeIframeAPIReady() {
for (var i = 0; i < 5; i++) {
add();
}
}
$("#add").click(add);
$("#remove").click(remove);
$("#pause").click(pause);
</script>
</body>
</html>
就像我上面说过的那样,当我最初制作它时,这种方法很好,我最近才注意到它不起作用。
Like I said above, this worked fine when I originally made it, and I only very recently noticed that it doesn't work.
推荐答案
start参数仍然有效。问题是,你为它计算了一个错误的值:错误的类型。
可能已更改:
提供有效的INTEGER值有效。 (如果允许分数,那么并非所有分数都有效。)
The start parameter still works. The problem is, that you compute a wrong value for it: wrong type. Maybe it has been changed: Supplying a valid INTEGER value works. (If fractions are allowed, then not all are valid.)
例如,如果你想要一个0到200之间的值,那么使用:
For example, if you want a value between 0 and 200 then use:
var maxVal = 200;
var startVal = Math.ceil((Math.random() * maxVal));
这篇关于使用YouTube API设置视频开始时间 - “开始”按钮playerVars选项用于工作,但现在不行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!