您好,登錄后才能下訂單哦!
HTML5的一個(gè)新特性就是內(nèi)置對(duì)多媒體的支持,<video> 元素很好用,也支持了不錯(cuò)的API接口,下面用了一個(gè)案例來(lái)說(shuō)明怎么對(duì)<video> 元素的控制。
- <!DOCTYPE >
- <html>
- <head>
- <title></title>
- <script src="js/jquery-1.7.1.min.js" type="text/javascript"></script>
- <script type="text/javascript">
- $(
- function() {
- $(":button").click(
- function() {
- var h;
- switch ($(":button").index($(this))) {
- case 0:
- if ($("video")[0].paused) {
- $("video")[0].play();
- $(this).val("暫停");
- }
- else {
- $("video")[0].pause();
- $(this).val("播放");
- }
- break;
- case 1:
- h = document.getElementsByTagName("video")[0].height == 0 ?
- document.getElementsByTagName("video")[0].videoHeight - 10 :
- document.getElementsByTagName("video")[0].height - 10; ;
- document.getElementsByTagName("video")[0].height = h;
- document.getElementsByTagName("video")[0].videoHeight = h;
- break;
- case 2:
- h = document.getElementsByTagName("video")[0].height == 0 ?
- document.getElementsByTagName("video")[0].videoHeight + 10 :
- document.getElementsByTagName("video")[0].height + 10; ;
- document.getElementsByTagName("video")[0].height = h;
- document.getElementsByTagName("video")[0].videoHeight = h;
- break;
- }
- }
- );
- }
- );
- $(
- function() {
- $("#video1").on(
- "canplay",
- function(e) {
- $(":button").removeAttr("disabled");
- console.log(e);
- }
- );
- $("#video1").on(
- "canplaythrough",
- function(e) {
- $("ol>li:eq(0)").html("全部加載完畢,你可以斷網(wǎng)看電影了!");
- console.log(e);
- }
- );
- $("#video1").bind(
- "playing waiting ended play pause",
- function(e) {
- var vObj = document.getElementById("video1");
- $("ol>li:eq(1)").html(vObj.duration + ":" + vObj.startTime + ":" + vObj.currentTime);
- console.log(e);
- }
- );
- $("#video1").on(
- "stalled",
- function(e) {
- $("ol>li:eq(2)").html("你的網(wǎng)絡(luò)不給力啊,正在等數(shù)據(jù)呢");
- console.log(e);
- }
- );
- $("#video1").on(
- "error",
- function(e) {
- switch (e.target.error.code) {
- case e.target.error.MEDIA_ERR_ABORTED:
- $("ol>li:eq(3)").html("媒體資源獲取異常");
- break;
- case e.target.error.MEDIA_ERR_NETWORK:
- $("ol>li:eq(3)").html("網(wǎng)絡(luò)錯(cuò)誤");
- break;
- case e.target.error.MEDIA_ERR_DECODE:
- $("ol>li:eq(3)").html("媒體解碼錯(cuò)誤");
- break;
- case e.target.error.MEDIA_ERR_SRC_NOT_SUPPORTED:
- $("ol>li:eq(3)").html("視頻格式被不支持");
- break;
- default:
- $("ol>li:eq(3)").html("這個(gè)是神馬錯(cuò)誤啊");
- break;
- }
- console.log(e);
- }
- );
- $("#video1").on(
- "suspend abort progress",
- function(e) {
- var vObj = document.getElementById("video1");
- $("ol>li:eq(1)").html(vObj.duration + ":" + vObj.startTime + ":" + vObj.currentTime);
- console.log(e);
- }
- );
- $("#video1").on(
- "progress error abort",
- function(e) {
- switch (e.target.readyState) {
- case 0:
- $("ol>li:eq(3)").html("當(dāng)前播放位置無(wú)有效媒介資源");
- break;
- case 1:
- $("ol>li:eq(3)").html("加載中,媒介資源確認(rèn)存在,但當(dāng)前位置沒(méi)有能夠加載到有效媒介數(shù)據(jù)進(jìn)行播放");
- break;
- case 2:
- $("ol>li:eq(3)").html("已獲取到當(dāng)前播放數(shù)據(jù),但沒(méi)有足夠的數(shù)據(jù)進(jìn)行播放");
- break;
- case 3:
- $("ol>li:eq(3)").html("已獲取到后續(xù)播放數(shù)據(jù),可以進(jìn)行播放");
- break;
- default:
- case 4:
- $("ol>li:eq(3)").html("可以進(jìn)行播放,且瀏覽器確認(rèn)媒體數(shù)據(jù)以某一種速度進(jìn)行加載,可以保證有足夠的后續(xù)數(shù)據(jù)進(jìn)行播放,而不會(huì)使瀏覽器的播放進(jìn)度趕上加載數(shù)據(jù)的末端");
- break;
- }
- console.log(e);
- }
- );
- }
- );
- </script>
- </head>
- <body>
- <video id="video1" autobuffer>
- <source src="video-test.mp4" type="video/mp4" />
- 對(duì)不起你的瀏覽器不支持HTML5的新特性,要不你下載一個(gè)
- <a href="http://info.msn.com.cn/ie9/">IE9</a>?
- </video>
- <input type="button" value="播放" disabled />
- <input type="button" value="縮小" disabled />
- <input type="button" value="放大" disabled />
- <ol>
- <li></li>
- <li></li>
- <li></li>
- <li></li>
- <li></li>
- </ol>
- </body>
- </html>
對(duì) Video的控制重要的方法就是play、paused、stop。重要的事件有:
canplay 通知用戶可以播放了,但不一定資源全部下載好
canplaythrough 資源都下載完畢了
error 出錯(cuò)時(shí)候
事件參數(shù)中有一個(gè)target對(duì)象,他有一個(gè)readyState值,可以得到不同的狀態(tài)信息。具體的值,可以通過(guò)開(kāi)發(fā)者工具獲得,或看相關(guān)文檔。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。