Hel*_*rld 17 javascript youtube youtube-api
我需要使用youtube的API加载多个视频.这是我第一次使用它,所以我不确定我做错了什么,但这就是我正在尝试的:
var player;
var player2;
function onYouTubePlayerAPIReady() {
player = new YT.Player('player', {
videoId: 'hdy78ehsjdi'
});
player2 = new YT.Player('player', {
videoId: '81hdjskilct'
});
}
Run Code Online (Sandbox Code Playgroud)
Vad*_*hev 26
由于onYouTubeIframeAPIReady函数应该只调用一次,因此可以使用以下方法:
初始化并保存ControlId,width,height,VideoId数组中的视频播放器信息()
调用onYouTubeIframeAPIReady函数来创建所有视频播放器
var playerInfoList = [{id:'player',height:'390',width:'640',videoId:'M7lc1UVf-VE'},{id:'player1',height:'390',width:'640',videoId:'M7lc1UVf-VE'}];
function onYouTubeIframeAPIReady() {
if(typeof playerInfoList === 'undefined')
return;
for(var i = 0; i < playerInfoList.length;i++) {
var curplayer = createPlayer(playerInfoList[i]);
}
}
function createPlayer(playerInfo) {
return new YT.Player(playerInfo.id, {
height: playerInfo.height,
width: playerInfo.width,
videoId: playerInfo.videoId
});
}
Run Code Online (Sandbox Code Playgroud)
Ega*_*ari 22
新YT.Player的第一个参数需要是要用视频的iframe替换的HTML元素(fe a DIV)的id.当您对这两个对象使用"播放器"时,您将两者都加载到同一个元素中.
<div id="ytplayer1"></div>
<div id="ytplayer2"></div>
<script>
var player;
var player2;
function onYouTubePlayerAPIReady() {
player = new YT.Player('ytplayer1', {
height: '390',
width: '640',
videoId: 'hdy78ehsjdi'
});
player2 = new YT.Player('ytplayer2', {
height: '390',
width: '640',
videoId: '81hdjskilct'
});
}
</script>
Run Code Online (Sandbox Code Playgroud)
Youtube API文档中描述了这些函数的参数:
https://developers.google.com/youtube/iframe_api_reference#Loading_a_Video_Player(编辑:更改为右侧链接)
小智 5
超文本标记语言
<div data-id="youtubevideoidhere" class="video"></div>
<div data-id="youtubevideoidhere" class="video"></div>
<div data-id="youtubevideoidhere" class="video"></div>
Run Code Online (Sandbox Code Playgroud)
视频 JS
// CREATE VIDEOS "CLASS" to handler videos
var Videos = (function() {
// VARIABLES
var $ = jQuery, // The jquery
players = [], // players array (to coltrol players individually)
queue = []; // videos queue (once api is ready, transform this into YT player)
// Constructor
function Videos() {}
// METHODS
// Add elements to queue
Videos.prototype.add = function($video) {
queue.push($video);
};
// Load YT API
Videos.prototype.loadApi = function() {
// jQuery get script
$.getScript("//www.youtube.com/iframe_api", function() {
// once loaded, create the onYouTubeIframeAPIReady function
window.onYouTubeIframeAPIReady = function() {
queue.forEach(function($video) {
// Create the YT player
var player = new YT.Player($video.get(0), {
'width': "100%",
'height': "100%",
'videoId': $video.data("id")
});
// add to players array
players.push(player);
});
};
});
};
return Videos;
})();
Run Code Online (Sandbox Code Playgroud)
然后,创建这样的视频
var videos = new Videos();
$('.video').each( function () {
videos.add( $(this) );
})
videos.loadApi();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
35104 次 |
| 最近记录: |