13

我正在尝试使用以下代码暂停和播放 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 中的演示

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

4

3 回答 3

23

播放器准备好后使用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();
    };
}
于 2012-07-01T17:09:57.763 回答
2

在您的 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

于 2014-03-04T19:53:10.700 回答
2
<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();
    }
}
于 2017-12-03T21:00:52.520 回答