如何可靠地连续播放多个视频?因为在播放2个视频之间有一个小的停顿或闪烁。
在我的特定示例中,我有3个视频。我需要一个接一个地无缝播放所有3个,我还需要循环中间视频任意次数(比如2到3次)。所有这一切都需要在不同的浏览器上无缝且一致地发生。
我已经尝试了所有的方法,从在视频端开始视频回放,到使用几个视频标签并隐藏和替换它们,我甚至试图在Flash中实现这一点,但遗憾的是没有一种方法有效,同样的问题也发生在当前的Flash中。
我已经看到这个(或类似的)问题被问了很多次,但我还没有看到一个可靠的解决方案。
有没有人知道解决这个问题的办法?
发布于 2015-12-07 23:50:24
在尝试了各种方法之后,我终于能够创建一个似乎可行的解决方案。我没有在老款浏览器或其他OSes上测试过,但它可以在最新版本的Chrome、IE、火狐和Opera上运行。(尽管更多关于这是否适用于其他系统的反馈将不胜感激)
这个想法是让所有3个视频输出帧到HTML5画布。原始视频会被提前隐藏和预加载,以避免加载之间出现停顿。
代码如下:
var playCounter = 0;
var clipArray = [];
var $video1 = $("#video1");
var $video2 = $("#video2");
var $video3 = $("#video3");
$video1.attr("src", "https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerEscapes.mp4");
$video2.attr("src", "https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerJoyrides.mp4");
$video3.attr("src", "https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerFun.mp4");
var timerID;
var $canvas = $("#myCanvas");
var ctx = $canvas[0].getContext("2d");
function stopTimer() {
window.clearInterval(timerID);
}
$('#startPlayback').click(function() {
stopTimer();
playCounter = $('#playbackNum').val();
clipArray = [];
// addd element to the end of the array
clipArray.push(1);
for (var i = 0; i < playCounter; i++) {
clipArray.push(2);
}
clipArray.push(3);
$video2[0].load();
$video3[0].load();
$video1[0].play();
});
function drawImage(video) {
//last 2 params are video width and height
ctx.drawImage(video, 0, 0, 640, 360);
}
// copy the 1st video frame to canvas as soon as it is loaded
$video1.one("loadeddata", function() {
drawImage($video1[0]);
});
// copy video frame to canvas every 30 milliseconds
$video1.on("play", function() {
timerID = window.setInterval(function() {
drawImage($video1[0]);
}, 30);
});
$video2.on("play", function() {
timerID = window.setInterval(function() {
drawImage($video2[0]);
}, 30);
});
$video3.on("play", function() {
timerID = window.setInterval(function() {
drawImage($video3[0]);
}, 30);
});
function onVideoEnd() {
//stop copying frames to canvas for the current video element
stopTimer();
// remove 1st element of the array
clipArray.shift();
//IE fix
if (!this.paused) this.pause();
if (clipArray.length > 0) {
if (clipArray[0] === 1) {
$video1[0].play();
}
if (clipArray[0] === 2) {
$video2[0].play();
}
if (clipArray[0] === 3) {
$video3[0].play();
}
} else {
// in case of last video, make sure to load 1st video so that it would start from the 1st frame
$video1[0].load();
}
}
$video1.on("ended", onVideoEnd);
$video2.on("ended", onVideoEnd);
$video3.on("ended", onVideoEnd);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<div id="border">
<video id="video1" width="640" height="360" hidden>
<source type="video/mp4">
Your browser does not support playing this Video
</video>
<video id="video2" width="640" height="360" hidden>
<source type="video/mp4">
Your browser does not support playing this Video
</video>
<video id="video3" width="640" height="360" hidden>
<source type="video/mp4">
Your browser does not support playing this Video
</video>
<div>
<canvas id="myCanvas" width="640" height="360"> </canvas>
</div>
<div role="controls">
<div>
<label>
Times the middle video will repeat itself:
</label>
</div>
<div>
<input id="playbackNum" value="1" />
</div>
<p>
<button id="startPlayback">Start</button>
</p>
</div>
</div>
请注意,代码不是很漂亮,会从一些清理和优化中受益,但至少这应该显示出解决问题的方法,并在HTML5中实现几个视频的无缝回放。还要确保在html文件位置包含jQuery源文件,代码才能正常工作。
发布于 2015-12-05 05:37:34
https://stackoverflow.com/questions/34097834
复制相似问题