如果你想這樣做,而不存儲服務器側的圖像是可能的,雖然有點笨重......使用畫布來記錄視頻的第一幀,然後覆蓋,超過了視頻元素。它可能會使用的URL畫布作爲海報(如video.poster = c.toDataURL();
)的來源,但需要正確的CORS設置(不知道,如果視頻是你自己的服務器上,如果你有控制這一點,所以採取了最安全的選擇)。這將工作最好的,如果視頻是正確編碼的流(所以MOOV原子是在視頻的前面,否則將是緩慢的繪製覆蓋 - 見this answer更多細節)
的HEAD
包含造型爲視頻和覆蓋(你將需要調整大小和位置,以滿足您的應用程序)
<head>
<style>
video_box{
float:left;
}
#video_overlays {
position:absolute;
float:left;
width:640px;
min-height:264px;
background-color: #000000;
z-index:300000;
}
</style>
</head>
在BODY
您需要div
的疊加和視頻。疊加div有一個onclick
處理程序來隱藏覆蓋並啓動視頻播放
<div id="video_box">
<div id="video_overlays" onclick="this.style.display='none';document.getElementById('video').play()"></div>
<video id="video" controls width="640" height="264">
<source src="BigBuck.mp4" type='video/mp4'>
</video>
</div>
</div>
最後,你需要的代碼,將加載視頻,尋求到第一幀和視覺加載到一個畫布,你再插入到覆蓋
<script>
function generateOverlay() {
video.removeEventListener('seeked',generateOverlay);/tidy up the event handler so it doesn't redraw the overlay every time the user manually seeks the video
var c = document.createElement("canvas");
var ctx = c.getContext("2d");
c.width = 640;
c.height = 264;
ctx.drawImage(video, 0, 0, 640, 264); // take the content of the video frame and place in canvas
overlay.appendChild(c); // insert canvas into the overlay div
}
// identify the video and the overlay
var video = document.getElementById("video");
var overlay = document.getElementById("video_overlays");
// add a handler that when the metadata for the video is loaded it then seeks to the first frame
video.addEventListener('loadeddata', function() {
this.currentTime = 0;
}, false);
// add a handler that when correctly seeked, it generated the overlay
video.addEventListener('seeked', function() {
// now video has seeked and current frames will show
// at the time as we expect
generateOverlay();
}, false);
// load the video, which will trigger the event handlers in turn
video.load();
</script>
可以添加你的代碼? –
有一個自動播放的解決方案,但它只能在IOS 10+工作 – Hokusai