好的,所以我在视频播放器上有一个事件监听器来监听“timeupdate”事件,该事件每秒发生 5-10 次,每次更新视频播放器的当前时间码时。我正在编写一个函数,允许我根据视频的播放位置在一定时间内做某事。IE - 在第 320 帧暂停视频并在第 420 帧取消暂停。
为了节省处理能力,我创建了一个名为 notStarted 的全局变量,它基本上告诉函数是启动事件(暂停视频)还是停止事件(取消暂停视频),这样当 notStarted 为真时,我们开始事件,当 notStarted是假的,我们结束事件。
这就是功能。它在开始事件后成功地将 notStarted 设为 false,但之后不会将其更改回 true。
更新:这是所有相关代码
var frameRate = 30;
var players=new Array(_V_("video_canvas_1"), _V_("video_canvas_2"));
var canv1 = players[0];
var canv2 = players[1];
var canv1Play = 1; // Autoplay is enabled, so we set the Play state to 1 off the bat.
var canv2Play = 1; // Autoplay is enabled, so we set the Play state to 1 off the bat.
var conchShell = _V_("conch"); // Lord of the Flies, Bitch!
var token = 0;
function currentFrame(e) {
return frameRate * e.currentTime();
console.log('currentTime * frameRate:' + frameRate * e.currentTime());
}
// Accepts player objects as arguments - canv1, canv2
function pause(){
for (var i = 0, j = arguments.length; i < j; i++){
arguments[i].pause();
console.log( arguments[i] + '1 is paused' ); /* TODO: REMOVE CONSOLE LOGS BEFORE DEPLOYMENT */
}
}
function play(){
for (var i = 0, j = arguments.length; i < j; i++){
arguments[i].play();
console.log( arguments[i] + '1 is playing' );
}
}
function syncSingleToConch(e) {
e.currentTime(conchShell.currentTime());
}
function syncToConchShell() {
async.parallel([
async.each(players, syncSingleToConch, function(err){ console.log('error with the each function'); }),
conchShell.currentTime(arguments[0].currentTime()),
async.each(players, playSingle, function(err){ console.log('error with the each function'); })
]);
}
function pauseAtKeyFrame(startStop, player, frameStart, frameEnd) {
if (startStop === "start") {
pause(player);
console.log('pausing player');
}
if (startStop === "stop") {
play(conchShell, canv1, canv2);
setTimeout(syncToConchShell(canv1), 3500);
startStop = "start";
console.log('playing player');
}
}
var currentEvent = 0;
var notStarted = true;
var frameEvents = [
{
"type": pauseShowModal,
"frameStart": 200,
"frameEnd": 220,
"canvas": canv1
},
{
"type": pauseAtKeyFrame,
"frameStart": 320,
"frameEnd": 420,
"canvas": canv1
},
{
"type": showDivAtTime,
"frameStart": 111,
"frameEnd": 311,
"canvas": canv1,
"div": "frame100"
}
];
function keyFrames() {
if (notStarted === true && frameEvents[currentEvent].frameStart < currentFrame(conchShell) && frameEvents[currentEvent].frameEnd > currentFrame(conchShell)) {
frameEvents[currentEvent].type("start", frameEvents[currentEvent].canvas, frameEvents[currentEvent].div);
notStarted = false;
console.log(notStarted);
} else if (notStarted === false && frameEvents[currentEvent].frameEnd < currentFrame(conchShell)) {
console.log(notStarted);
frameEvents[currentEvent].type("stop", frameEvents[currentEvent].canvas, frameEvents[currentEvent].div);
currentEvent++;
notStarted = true;
}
}
conchShell.addEvent("timeupdate", keyFrames);