使用 Javascript 浏览 Vimeo 视频
Moving through Vimeo videos with Javascript
我想按 JavaScript 顺序播放 Vimeo 视频,并连续播放和循环播放项目。以下代码在第一个视频末尾移动到第二个视频,但在第二个视频末尾未调用相同的函数。只有第一个视频的结尾在 Javascript 控制台中显示 Called!
,而不是第二个视频的结尾。
Vimeo Player SDK 有 player.getVideoId()
但没有 player.setVideoId()
所以我使用 hack 来加载下一个视频:设置 iframe 的源并重新启动 Vimeo 播放器。我想知道是不是这个问题,视频结尾的监听器附加到丢弃的播放器上。
此代码的另一个问题是它退出全屏以加载下一个视频。
我尝试 this CodePen 在任何元素而不是 iframe 上设置 Vimeo 播放器,但无法更改播放的视频。
使用 Vimeo Player SDK 更改视频的更简洁或有效的方法是什么?
HTML是:
<div class="vimeo">
<div class="resp-iframe-container container-centered">
<iframe class="resp-iframe" scrolling="no" frameborder="no" src="https://player.vimeo.com/video/238301525" allowfullscreen="" data-ready="true"></iframe></div></div>
而 javascript 是:
var l = ["238301525", "496371201", "238301525", "496371201", "..."];
window.current = 0;
var increment = function() {
window.current += 1;
if (window.current == l.length) {
window.current = 0;
}
};
var decrement = function() {
window.current -= 1;
if (window.current < 0) {
window.current = l.length - 1;
}
};
prevA.addEventListener("click", function() {
decrement();
loadPlayer();
});
nextA.addEventListener("click", function() {
increment();
console.log("here!");
loadPlayer();
console.log("there!");
});
var loadPlayer = function() {
console.log("Called!");
var iframe = document.querySelector('iframe');
iframe.src = "https://player.vimeo.com/video/" + l[window.current];
var player = new Vimeo.Player(iframe);
player.autoplay = true;
var treatEvent = function(data, eventLabel) {
// Custom code...
if ("end" == eventLabel) {
console.log("incrementing automatically");
increment();
loadPlayer();
}
};
var onPlay = function(data) {
treatEvent(data, "play");
}
var onPause = function(data) {
treatEvent(data, "pause");
}
var onSeeked = function(data) {
treatEvent(data, "seeked");
}
var onEnd = function(data) {
treatEvent(data, "end");
}
player.on('play', onPlay);
player.on('pause', onPause);
player.on('seeked', onSeeked);
player.on('ended', onEnd);
setTimeout(function() {
//console.log("Trying to play...");
player.play().then(function() {
// the video was played
console.log("success: video played");
}).catch(function(error) {
console.log("Error! " + error.name);
});
}, 1000);
}
loadPlayer();
似乎 Vimeo Player 会跟踪它是否已初始化,在 HTML 元素中添加标签 data-vimeo-initialized="true"
。
一种解决方案是使用 Vimeo Player SDK function .loadVideo()
:
var song_iframe = document.querySelector("iframe#song_video");
var song_player = new Vimeo.Player(song_iframe);
var on_song_end = function(data) {
// Logic to get the new id.
song_player.loadVideo(newId).then(function() {
song_player.play();
});
};
song_player.on("ended", on_song_end);
我想按 JavaScript 顺序播放 Vimeo 视频,并连续播放和循环播放项目。以下代码在第一个视频末尾移动到第二个视频,但在第二个视频末尾未调用相同的函数。只有第一个视频的结尾在 Javascript 控制台中显示 Called!
,而不是第二个视频的结尾。
Vimeo Player SDK 有 player.getVideoId()
但没有 player.setVideoId()
所以我使用 hack 来加载下一个视频:设置 iframe 的源并重新启动 Vimeo 播放器。我想知道是不是这个问题,视频结尾的监听器附加到丢弃的播放器上。
此代码的另一个问题是它退出全屏以加载下一个视频。
我尝试 this CodePen 在任何元素而不是 iframe 上设置 Vimeo 播放器,但无法更改播放的视频。
使用 Vimeo Player SDK 更改视频的更简洁或有效的方法是什么?
HTML是:
<div class="vimeo">
<div class="resp-iframe-container container-centered">
<iframe class="resp-iframe" scrolling="no" frameborder="no" src="https://player.vimeo.com/video/238301525" allowfullscreen="" data-ready="true"></iframe></div></div>
而 javascript 是:
var l = ["238301525", "496371201", "238301525", "496371201", "..."];
window.current = 0;
var increment = function() {
window.current += 1;
if (window.current == l.length) {
window.current = 0;
}
};
var decrement = function() {
window.current -= 1;
if (window.current < 0) {
window.current = l.length - 1;
}
};
prevA.addEventListener("click", function() {
decrement();
loadPlayer();
});
nextA.addEventListener("click", function() {
increment();
console.log("here!");
loadPlayer();
console.log("there!");
});
var loadPlayer = function() {
console.log("Called!");
var iframe = document.querySelector('iframe');
iframe.src = "https://player.vimeo.com/video/" + l[window.current];
var player = new Vimeo.Player(iframe);
player.autoplay = true;
var treatEvent = function(data, eventLabel) {
// Custom code...
if ("end" == eventLabel) {
console.log("incrementing automatically");
increment();
loadPlayer();
}
};
var onPlay = function(data) {
treatEvent(data, "play");
}
var onPause = function(data) {
treatEvent(data, "pause");
}
var onSeeked = function(data) {
treatEvent(data, "seeked");
}
var onEnd = function(data) {
treatEvent(data, "end");
}
player.on('play', onPlay);
player.on('pause', onPause);
player.on('seeked', onSeeked);
player.on('ended', onEnd);
setTimeout(function() {
//console.log("Trying to play...");
player.play().then(function() {
// the video was played
console.log("success: video played");
}).catch(function(error) {
console.log("Error! " + error.name);
});
}, 1000);
}
loadPlayer();
似乎 Vimeo Player 会跟踪它是否已初始化,在 HTML 元素中添加标签 data-vimeo-initialized="true"
。
一种解决方案是使用 Vimeo Player SDK function .loadVideo()
:
var song_iframe = document.querySelector("iframe#song_video");
var song_player = new Vimeo.Player(song_iframe);
var on_song_end = function(data) {
// Logic to get the new id.
song_player.loadVideo(newId).then(function() {
song_player.play();
});
};
song_player.on("ended", on_song_end);