如果您知道视频 ID,则可以以编程方式呈现视频播放器:
加载 youtube API 并定义占位符 (#playList)
<script src='//www.youtube.com/player_api'></script>
<div id='playList'>
<!--// placeholder -->
</div>
页面加载时:
var playList = [
{ videoId: 'aEHIgjoueeg', player: null, $el: null },
{ videoId: 'c5NcaVp2-5M', player: null, $el: null },
{ videoId: 'IA88AS6Wy_4', player: null, $el: null }
];
function onStateChange (e) {
if (YT.PlayerState.PLAYING === e.data) {
console.log(this, e);
// stop others
$.each(playList, function (i, item) {
if (item.videoId === this.videoId) {
return true;
}
if (item.player) {
item.player.stopVideo();
}
}.bind(this));
}
}
var $playList = $('#playList');
$.each(playList, function (i, item) {
item.$el = $('<div id="'+ item.videoId +'"></div>');
$playList.append(item.$el);
item.player = new window.YT.Player(item.videoId, {
width: 300, height: 200,
videoId: item.videoId,
playerVars: { autoplay: 0 },
events: {
onStateChange: onStateChange.bind(item)
}
});
});
onStateChange 内部:如果当前视频正在播放,则停止其他视频
http://jsfiddle.net/34hysaes/