gpt4 book ai didi

api - 使用onYouTubeIframeAPIReady()加载两个YouTube视频

转载 作者:行者123 更新时间:2023-12-03 06:10:56 25 4
gpt4 key购买 nike

我正在将JS的YouTube视频加载到div中
JS:

function onYouTubeIframeAPIReady() {
player = new YT.Player('video1', {
width: 600,
height: 400,
videoId: 'pDMwa1NYceY',
playerVars: {
color: 'white',
controls: 0,
showinfo: 0
},
events: {
onReady: initialize
}
});
}

HTML:
<div id="video1"></div>

可以,但是现在我想将另一个视频加载到同一页面上的另一个DIV中

HTML:
<div id="video1"></div>
<div id="video2"></div>

JS:
function onYouTubeIframeAPIReady() {
player = new YT.Player('video1', {
width: 600,
height: 400,
videoId: 'XFmRL-Vuwtc',
playerVars: {
color: 'white',
controls: 0,
showinfo: 0
},
player = new YT.Player('video2', {
width: 600,
height: 400,
videoId: 'fQsHVCDn-bs',
playerVars: {
color: 'white',
controls: 0,
showinfo: 0
},
events: {
onReady: initialize
}
});
}

我该如何工作?

我为什么要这样做?我想用JS同时控制两个YouTube视频。

最佳答案

首先,对于要应用的每个iframe,您都应使用不同的变量。也就是说,如果您要有两个YouTube iframe,则必须添加两个变量。

下一个示例显示了两个YouTube iframe(每个iframe具有不同的视频)和两个按钮:“恢复”和“暂停”。

You'll have to test in your own enviroment because it seems the code-snippet doesn't allow load the YouTube iframe API and I couldn't so far make a completely functional jsfiddle, but this code will give you a hint about how I manage the "control" of the two YouTube loaded iframes:



/*
Sources:
https://developers.google.com/youtube/iframe_api_reference?hl=es-419
https://stackoverflow.com/a/17843719/4092887
https://stackoverflow.com/a/11284045/4092887
https://stackoverflow.com/a/17857716/4092887
*/

var tag = document.createElement('script');

tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

// Variables of the divs (where the YouTube iframe will load):
var player;
var multip;

function onYouTubeIframeAPIReady() {

// Div player:
player = new YT.Player('player', {
height: '360',
width: '640',
videoId: 'M7lc1UVf-VE',
events: {
'onReady': onPlayerReady
}
});

// Div "multip":
multip = new YT.Player('multip', {
height: '360',
width: '640',
videoId: '7gwO8-oqwFw',
events: {
'onReady': onPlayerReady2
}
});

// Add onclick events to HTML anchor controls for play and
// pause the videos.
document.getElementById('resume').onclick = function() {
play_video();
};
document.getElementById('pause').onclick = function() {
pause_video();
};
}

// Div "player" - The API will call this function when the video player is ready.
function onPlayerReady(event) {
event.target.playVideo();
}

// Div "multip" - The API will call this function when the video player is ready.
function onPlayerReady2(event) {
event.target.playVideo();
}

// Function for play all the loaded iframes: those div are: ("player" and "multip").
function play_video() {
player.playVideo();
multip.playVideo();
}


// Function for pause all the loaded iframes: those div are: ("player" and "multip").
function pause_video() {
player.pauseVideo();
multip.pauseVideo();
}

// Function for reset (start at 0:00) all the loaded iframes: those div are: ("player" and "multip").
function reset_video() {
player.stopVideo();
multip.stopVideo();
}
<script src="https://www.youtube.com/iframe_api"></script>
<!-- These divs will contain the iframe YouTube player: -->
<div id="player"></div>
<div id="multip"></div>

<a href="#" id="pause">Pause</a>
<a href="#" id="resume">Resume</a>

关于api - 使用onYouTubeIframeAPIReady()加载两个YouTube视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54062545/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com