我有一个Youtube视频嵌入幻灯片,当用户点击img缩略图时,我想暂停一下:
<li><iframe width="430" height="241" src="http://www.youtube.com/watch?v=XjUz8IT0CYg?enablejsapi=1&theme=light&showinfo=0" frameborder="0" allowfullscreen id="my-video"></iframe></li>我已经浏览过Youtube API,但我很困惑如何开始工作。
当我将?enablejsapi追加到YouTube video id的末尾时,API JS会自动加载吗?
这是我的JS:
var player1 = document.getElementById('my-video');
$('img').click(function () {
player1.pauseVideo();
})编辑:
以下是我根据马特下面的回答所做的,对于任何想知道的人来说:
<li><iframe width="430" height="241" src="http://www.youtube.com/embed/XjUz8IT0CYg?enablejsapi=1&theme=light&showinfo=0" frameborder="0" allowfullscreen id="player-1"></iframe></li>
<li><iframe width="430" height="241" src="http://www.youtube.com/embed/HVhSasnVjMQ?enablejsapi=1&theme=light&showinfo=0" frameborder="0" allowfullscreen id="player-2"></iframe></li>然后我的JS:
var tag = document.createElement('script');
tag.src = "//www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
// Create YouTube player(s) after the API code downloads.
var player1;
var player2;
function onYouTubeIframeAPIReady() {
player1 = new YT.Player('player-1');
player2 = new YT.Player('player-2');
}然后在document.ready
$(document).ready(function () {
$(".slideshow-1 img").click(function () {
player1.stopVideo();
});
$(".slideshow-2 img").click(function () {
player2.stopVideo();
});
}发布于 2012-09-20 23:45:38
嵌入字符串中的附加?enablejsapi不会自动包含API代码。它只在API中注册特定的嵌入。
您想在这里阅读第一个示例:reference
onYouTubeIframeAPIReady()是在API准备就绪时触发的。YT.Player对象pauseVideo()。在启动onYouTubeIframeAPIReady()之前,您很可能希望禁用img上的任何事件。
发布于 2013-01-15 23:27:46
如果希望将JS保存在外部文件中,请使用:
<iframe id="player1" class="ytplayer" width="430" height="241" src="http://www.youtube.com/embed/HVhSasnVjMQ?enablejsapi=1&theme=light&showinfo=0" frameborder="0" allowfullscreen></iframe>
<iframe id="player2" class="ytplayer" width="430" height="241" src="http://www.youtube.com/embed/HVhSasnVjMQ?enablejsapi=1&theme=light&showinfo=0" frameborder="0" allowfullscreen></iframe>JS
var yt_int, yt_players={},
initYT = function() {
$(".ytplayer").each(function() {
yt_players[this.id] = new YT.Player(this.id);
});
};
$.getScript("//www.youtube.com/player_api", function() {
yt_int = setInterval(function(){
if(typeof YT === "object"){
initYT();
clearInterval(yt_int);
}
},500);
});控制视频
yt_players['player_id'].playVideo();我这样做的原因是因为我通过CMS动态加载视频,并且它们在一个覆盖层中打开。我将关闭/打开覆盖功能设置为播放/暂停视频。
发布于 2018-03-15 06:49:19
我有另一个解决办法:
<iframe id="player1" class="ytplayer" width="430" height="241" src="http://www.youtube.com/embed/HVhSasnVjMQ?enablejsapi=1&theme=light&showinfo=0" frameborder="0" allowfullscreen></iframe>
<iframe id="player2" class="ytplayer" width="430" height="241" src="http://www.youtube.com/embed/HVhSasnVjMQ?enablejsapi=1&theme=light&showinfo=0" frameborder="0" allowfullscreen></iframe>和
$('.ytplayer').each(function(){
//this.contentWindow.postMessage('{"event":"command","func":"' + 'stopVideo' + '","args":""}', '*');
this.contentWindow.postMessage('{"event":"command","func":"' + 'pauseVideo' + '","args":""}', '*')
});https://stackoverflow.com/questions/12522291
复制相似问题