暂停YouTube视频,youtube api



我正在尝试使用以下代码暂停并播放YouTube视频,这几乎是Youtube API页面的副本:

// 2. This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');
tag.src = "http://www.youtube.com/player_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
// 3. This function creates an <iframe> (and YouTube player)
//    after the API code downloads.
var player;
function onYouTubePlayerAPIReady() {
    player = new YT.Player('player', {
        height: '315',
        width: '560',
        videoId: 'bpOR_HuHRNs',
    });
}

这是jsFiddle中的一个演示

但是,它不起作用。有人知道如何做到这一点吗?

使用player.playVideo();(恢复)和player.pauseVideo();(暂停)一旦玩家准备好:http://jsfiddle.net/4WPmY/6/

function onYouTubePlayerAPIReady() {
    player = new YT.Player('player', {
        height: '315',
        width: '560',
        videoId: 'bpOR_HuHRNs',
    });
    document.getElementById('resume').onclick = function() {
        player.playVideo();
    };
    document.getElementById('pause').onclick = function() {
        player.pauseVideo();
    };
}

在你的HTML中,有一些按钮来控制视频:

<input type="button" id="play">
<input type="button" id="pause">

使用 jQuery,绑定事件侦听器(单击)以触发播放器对象上的函数:

$(function() {
  $('#play').click(function() {
    player.playVideo();
  });
  $('#pause').click(function() {
    player.pauseVideo();
  });
});

我使用YouTube的Player and Data api构建了一个应用程序。采取您需要的:https://github.com/HunterMeyer/YouTV

<div id="player"></div>
<a href="#" id="resume">Play</a>

在此代码中,我添加了"恢复"和"暂停"按钮: 示例

// 2. This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/player_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
// 3. This function creates an <iframe> (and YouTube player)
//    after the API code downloads.
var player;
function onYouTubePlayerAPIReady() {
    player = new YT.Player('player', {
        height: '315',
        width: '560',
        videoId: '0Bmhjf0rKe8',
        events: {
            'onStateChange': onPlayerStateChange
        }
    });
    document.getElementById('resume').onclick = function() {
        PlayPause();
        return false;
    };
}
// 4. This function change name of tag click.
var playerState;
function onPlayerStateChange(event) {
    var getId = document.getElementById('resume');
    if(event.data === 0) {
        getId.innerText = 'Play';
    }
  else if(event.data === 1) {
        getId.innerText = 'Pause';
    }
  else if(event.data === 2) {
        getId.innerText = 'Resume';
    }
  else if(event.data === 3) {
        getId.innerText = 'Loading...';
    }
    playerState = event.data;
}
// 5. This function Play/Pause the video.
function PlayPause() {
    if(playerState == '1') {
        player.pauseVideo();
    }
    else {
        player.playVideo();
    }
}

相关内容

  • 没有找到相关文章

最新更新