Youtube视频播放器播放最后的视频只
Youtube Video Player Playing Last Video Only
使用google嵌入式播放器与JS
我有两个玩家在.php page
上
第一个视频不播放,第二个视频播放。
我很确定,因为我用第二个函数重写了第一个函数。我做错了什么?
0视频
<!-- 1. The <iframe> (and video player) will replace this <div> tag. -->
<div id="player0"></div>
<script>
// 2. This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_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 player0;
function onYouTubeIframeAPIReady() {
player0 = new YT.Player('player0', {
height: '390',
width: '640',
videoId: 'M7lc1UVf-VE',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
// 4. The API will call this function when the video player is ready.
function onPlayerReady(event) {
event.target.playVideo();
}
// 5. The API calls this function when the player's state changes.
// The function indicates that when playing a video (state=1),
// the player should play for six seconds and then stop.
var done = false;
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.PLAYING && !done) {
setTimeout(stopVideo, 6000);
done = true;
}
}
function stopVideo() {
player0.stopVideo();
}
</script>
视频1 <!-- 1. The <iframe> (and video player) will replace this <div> tag. -->
<div id="player1"></div>
<script>
// 2. This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_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 player1;
function onYouTubeIframeAPIReady() {
player1 = new YT.Player('player1', {
height: '390',
width: '640',
videoId: 'M7lc1UVf-VE',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
// 4. The API will call this function when the video player is ready.
function onPlayerReady(event) {
event.target.playVideo();
}
// 5. The API calls this function when the player's state changes.
// The function indicates that when playing a video (state=1),
// the player should play for six seconds and then stop.
var done = false;
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.PLAYING && !done) {
setTimeout(stopVideo, 6000);
done = true;
}
}
function stopVideo() {
player1.stopVideo();
}
</script>
你是对的,你正在覆盖你的功能。例如,onYouTubeIframeAPIReady()
只会创建第二个玩家。你可以猜到,它应该这样写:
function onYouTubeIframeAPIReady() {
//declare your first player
//declare your second player
}
Youtube API文档中的done
变量仅适用于一个播放器。如果需要多个变量,则应该创建多个变量或一个数组。但为了清楚起见,我就不细说了。
创建多个玩家并让他们玩的例子:
<div id="player0"></div>
<div id="player1"></div>
<script>
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player0, player1;
function onYouTubeIframeAPIReady() {
player0 = new YT.Player('player0', {
height: '390',
width: '640',
videoId: 'M7lc1UVf-VE',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
player1 = new YT.Player('player1', {
height: '390',
width: '640',
videoId: 'M7lc1UVf-VE',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
function onPlayerReady(event) {
event.target.playVideo();
}
function onPlayerStateChange(event) {
//do something with 'event.target' if you need to
}
</script>
JS Fiddle Demo
需要更多的例子或解释?
相关文章:
- Brightcove获取/显示HTML中的当前视频标题和描述
- 强制浏览器更新缓存的HTML5视频对象
- 可以使用带有json回调的html5视频标签
- 谷歌地图-数据库中的标记只显示最后一行的信息
- 用链接替换嵌入的HTML5视频
- ReactJS:如何使用react正确播放带有onClick事件的html5视频
- 资源管理器9上的html5视频缓存
- 灯箱中的jQuery视频
- Angular/Rails从表中获取最后保存的数据
- 使用video-js构建一个包含字符串的HTML视频
- 使用RecordRTC录制的WebRTC视频在IE和Safari中显示错误
- 如何构建自己的 VAST 视频播放器
- JS中的HTML5视频标签错误检测
- 在网站上嵌入最新的YouTube视频
- 为什么我的php数组只返回最近/最后发布的活动
- 检查最后键入的字符是否为空格
- 使用javascript的第三方视频检测
- Javascript-如何清除最后绘制的矩形,以便在'拖动'以创建矩形
- 为什么'我的HTML5视频控制按钮使用Javascript
- 当页面上的所有视频都已完全加载时,执行javascript