在JavaScript中暂停YouTube iFrame API

时间:2012-09-20 23:17:42

标签: javascript jquery youtube youtube-api

我在幻灯片中嵌入了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();
})

修改

以下是我根据Matt的答案做的事情,对于任何想知道的人来说:

<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();
    });
}

3 个答案:

答案 0 :(得分:19)

如果您想将JS保存在外部文件中:

<强> HTML

<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动态加载视频并以叠加方式打开。我设置了关闭/打开叠加功能来播放/暂停视频。

答案 1 :(得分:13)

在嵌入字符串中附加?enablejsapi不会自动包含API代码。它只注册API的特定嵌入。

您想在此处阅读第一个示例:https://developers.google.com/youtube/iframe_api_reference

  1. 异步代码段下载并执行YT iframe API
  2. API准备好后会触发
  3. onYouTubeIframeAPIReady()
  4. 创建新的YT.Player对象
  5. 您的代码现在可以在您的播放器对象上调用pauseVideo()
  6. onYouTubeIframeAPIReady()被解雇之前,您很可能想要禁用img上的所有活动。

答案 2 :(得分:1)

我有另一个解决方案:

<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":""}', '*')
});