Javascript 暂停 youtube 视频、youtube api
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11283871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pause youtube video, youtube api
提问by halliewuud
I'm trying to pause and play YouTube videos with the following code which is pretty much a copy from the Youtube API page:
我正在尝试使用以下代码暂停和播放 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',
});
}
However, it's not working. Anyone have a idea how to do this?
但是,它不起作用。任何人都知道如何做到这一点?
回答by Rob W
Use player.playVideo();
(resume) and player.pauseVideo();
(pause) once the player is ready: http://jsfiddle.net/4WPmY/6/
播放器准备好后使用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();
? ??};
}
回答by Hunter
In your HTML, have some buttons to control the video:
在您的 HTML 中,有一些按钮来控制视频:
<input type="button" id="play">
<input type="button" id="pause">
Using jQuery, bind an event listener (the click) to trigger a function on your player object:
使用 jQuery,绑定一个事件侦听器(单击)以触发播放器对象上的函数:
$(function() {
$('#play').click(function() {
player.playVideo();
});
$('#pause').click(function() {
player.pauseVideo();
});
});
I built an app using YouTube's Player and Data api. Take what you need: https://github.com/HunterMeyer/YouTV
我使用 YouTube 的播放器和数据 api 构建了一个应用程序。拿你需要的东西:https: //github.com/HunterMeyer/YouTV
回答by jmsmarcelo
<div id="player"></div>
<a href="#" id="resume">Play</a>
In this code I added the Resume and Pause buttons in one: Example
在这段代码中,我将恢复和暂停按钮合二为一:示例
// 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();
}
}