簡介:javascript
瀏覽器支持:html
<video src='../test.webm' controls> <object data='videoplayer.swf' type='application/x-shockwave-flash'> <param name="movie" value="video.swf"> </object> </video>
以插件方式播放視頻的代碼爲備選內容;不過考慮到各類瀏覽器,可能須要多種視頻格式;java
WebVTT文件: 一個WebVTT(.vtt)文件是一個簡單的純文本,裏面包含了幾種類型的視頻信息:web
test.vtt --------- 1 00:00:01.000 --> 00:00:18.000 At the left we can see... test.html ------------ <video src='../test.webm' controls> <track src="test.vtt" kind="subtitles" srclang="en" label="English" /> </video>
理解媒體元素:算法
<audio controls> <source src = 'test.ogg'> <source src = 'test.mp3'> </audio>
來源列表的順序,最好按照用戶由高到低或者服務器消耗由低到高; canvas
使用audio瀏覽器
<body> <audio id='musci'> <source src='test.ogg'> <source src='test.mp3'> </audio> <button id='toggle' onclick='toggleSound()'>Play</button> </body> <script type="text/javascript"> function toggleSound() { var music = document.getElementById('musci'); var toggle = document.getElementById('toggle'); if(music.paused) { music.play(); toggle.innerHTML = 'Pause'; } else { music.pause(); toggle.innerHTML = 'Play'; } } </script>
使用video服務器
<body> <video src='./test.webm' controls id='movies' autoplay oncanplay='startVideo()' onended='stopTimeline()'> <object data='videoplayer.swf' type='application/x-shockwave-flash'> <param name="movie" value="video.swf"> </object> </video> <canvas id='timeline' width='400px' height='300px'> </canvas> </body> <script type="text/javascript"> //設置幀更新的間隔 var updateInterval = 5000; //時序中幀的尺寸 var frameWidth = 100; var frameHeight = 75; //時序的總幀數 var frameRows = 4; var frameColumns = 4; var frameGrid = frameRows * frameColumns; //當前幀 var frameCount = 0; //播放完後取消計時器 var intervalId; var videoStarted = false; function startVideo() { //確保第一次播放才觸發 if(videoStarted) { return } videoStarted = true; updateFrame(); intervalId = setInterval(updateFrame, updateInterval); var timeline = document.getElementById('timeline'); timeline.onclick = function(evt) { var video = document.getElementById('movies'); //計算出第幾個,0開始計算 var offX = evt.layerX; var offY = evt.layerY; var clickedFrame = Math.floor(offY / frameHeight) * frameRows; clickedFrame += Math.floor(offX / frameWidth) ; var totalTimes = Math.floor(frameCount / 16) * 16 + clickedFrame; if(clickedFrame > (frameCount % 16)) { totalTimes -= 16; } video.currentTime = totalTimes * updateInterval / 1000; } } //把幀繪製到畫布上 function updateFrame() { var video = document.getElementById('movies'); var timeline = document.getElementById('timeline'); var ctx = timeline.getContext('2d'); //當前播放位置 var framePosition = frameCount % frameGrid; var frameX = (framePosition % frameColumns) * frameWidth; var frameY = (Math.floor(framePosition / frameRows)) * frameHeight; //canvas的繪製程序能夠將視頻源看成圖像繪製 ctx.drawImage(video, 0, 0, 400, 300, frameX, frameY, frameWidth, frameHeight); frameCount++; } function stopTimeline() { clearInterval(intervalId); } </script>
<video src='./test.webm' controls id='movies' autoplay onmouseover='this.play()'onmouseout='this,pause()'> <object data='videoplayer.swf' type='application/x-shockwave-flash'> <param name="movie" value="video.swf"> </object> </video>