同步多个YouTube视频's
Synchronizing multiple YouTube video's
同时播放多个YouTube的最佳方式是什么?我希望它们在毫秒内同步,因此不受缓冲问题或不同长度广告的影响。
更新1:
我认为如果我能得到以下问题的答案,我的问题就会得到充分的回答:
当视频能够播放时(视频已缓冲到足以播放/广告未播放/视频未因任何其他原因暂停),如何使用YouTube的javascript API进行检测?
更新2:
YouTube同步的基本思想已经由SwigView完成。唯一缺少的是视频的同步更精确,而SwigView在实现这一点上做得并不好。
我开始怀疑在目前的API下这是可能的,我正在寻找替代方法。
通过定期测量和调整两个播放器之间的时间差,可以在0.2s或更好的公差范围内同步两个YouTube iFrame API播放器。例如,通过将一个播放器的播放速度加倍或减半,并在X/2毫秒后将其设置回正常速度,可以非常准确地调整X毫秒的时间差。用户交互(停止、播放、暂停)的帮助程序可以从常规API曲目中添加。他们也覆盖广告,因为他们暂停了球员。
澄清代码:
script.js
// the players
var player1;
var player2;
// the rules
var syncThreshold=0.2; // seconds, threshold for an acceptable time difference to prevent non-stop syncing
var jumpThreshold=2; // seconds, threshold for a time difference that should be corrected by a rough jump
var jumpDeadTime=500; // milliseconds, a dead time in which we don't sync after a jump
// timeouts and intervals
var timeSyncInterval;
var syncActionTimeout=undefined;
// The YouTube API calls this once it's ready
function onYouTubeIframeAPIReady() {
player1 = new YT.Player('somediv1', {
videoId: 'zkv-_LqTeQA',
events: {
onReady: syncTime,
onStateChange: syncStateChange
}
});
player2 = new YT.Player('somediv2', {
videoId: 'zkv-_LqTeQA'
});
}
// the syncing magic
function syncTime(){
// making sure the syncing interval has not been set already for some reason
clearInterval(timeSyncInterval);
// setting a 1s interval in which we check it the players are in sync and correct in necessary
timeSyncInterval = setInterval(function () {
// if the timeout is already set, we are already trying to sync the players, so we don't have to do it again
if(syncActionTimeout==undefined){
// measure the time difference and calculate the duration of the sync-action
var time1=player1.getCurrentTime();
var time2=player2.getCurrentTime();
var timeDifference=time2-time1;
var timeDifferenceAmount=Math.abs(timeDifference);
var syncActionDuration=1000*timeDifferenceAmount/2;
if(timeDifferenceAmount>jumpThreshold){
// the players are too far apart, we have to jump
console.log("Players are "+timeDifferenceAmount+" apart, Jumping.");
player2.seekTo(player1.getCurrentTime());
// we give the player a short moment to start the playback after the jump
syncActionTimeout=setTimeout(function () {
syncActionTimeout=undefined;
},jumpDeadTime);
}else if(timeDifference>syncThreshold){
// player 2 is a bit ahead of player 1, slowing player 2 down
console.log("Player 2 is "+timeDifference+"s ahead of player 1. Syncing.");
player2.setPlaybackRate(0.5);
// setting a timeout that fires precisely when both players are sync
syncActionTimeout=setTimeout(function () {
// the players should be sync now, so we can go back to normal speed
player2.setPlaybackRate(1);
syncActionTimeout=undefined;
},syncActionDuration);
}else if(timeDifference<-syncThreshold){
console.log("Player 1 is "+(-timeDifference)+"s ahead of player 2. Syncing.");
// player 1 is bit ahead of player 2, slowing player 2 down
player2.setPlaybackRate(2);
// setting a timeout that fires precisely when both players are sync
syncActionTimeout=setTimeout(function () {
// the players should be sync now, so we can go back to normal speed
player2.setPlaybackRate(1);
// undefining the timeout to indicate that we're done syncing
syncActionTimeout=undefined;
},syncActionDuration);
}
}
},1000);
}
// a little helper to deal with the user
function syncStateChange(e){
if(e.data==YT.PlayerState.PLAYING){
player2.seekTo(player1.getCurrentTime());
player2.playVideo();
}else if(e.data==YT.PlayerState.PAUSED){
player2.seekTo(player1.getCurrentTime());
player2.pauseVideo();
}
}
index.html
<!DOCTYPE html>
<html>
<head>
<title>Sync Two Youtube Videos</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<!-- CDN -->
<script type="text/javascript" src="//www.google.com/jsapi"></script>
<script type="text/javascript" src="https://apis.google.com/js/client.js?onload=onJSClientLoad"></script>
<script src="https://www.youtube.com/iframe_api"></script>
<!-- HOSTED -->
<script src="script.js"></script>
</head>
<body>
<div id="somediv1"></div>
<div id="somediv2"></div>
</body>
</html>
相关文章:
- 是否可以使用iframe API在iOS浏览器上播放youtube视频
- html5视频中的Youtube类型注释
- 使用javascript将视频从我的android应用程序上传到youtube
- 如何用低音量自动启动Youtube视频(iframe)
- 使用HTML5播放器时使用Javascript更改Youtube视频
- 有没有办法在dailymotion播放器的新测试版中播放youtube视频
- 在Android Kitkat上模拟鼠标点击Youtube视频
- 我们如何在dailymotion播放器的新测试版中播放youtube视频
- Lightbox关闭时停止播放Youtube视频
- 放大弹出生成“;找不到文件”;YouTube视频的错误
- 如何获取youtube视频的缩略图
- 如何重定向到我的html网站youtube嵌入视频打开在youtube应用程序
- 从我的频道中提取最后3个youtube视频
- 检测YouTube上的360度视频
- 如何修改此功能以获得最受欢迎的视频?(YouTube API v3)
- Iframe视频(Youtube)滑动
- 检索播放列表视频YouTube API的持续时间、视频图片和视图
- 滑动方块益智游戏,视频(youtube)背景
- 就浏览器兼容性而言,ifram或
- 如何获得第一个视频结束后的最后一个视频YouTube api