Javascript實現視頻輪播

來源:互聯網
上載者:User

最近客戶要求用Javascript實現視頻輪播:(單視頻,多視頻均可以)

      有興趣的同學可以參開一下

      下面寫了一個程式實現多視頻輪播,pc端與移動端均可以實現,(單視頻播放實現的原理也是一樣)

      只要在polyv平台註冊一個帳號,上傳幾個視頻,就可以利用這段代碼了。


實現的原理是,拿到在polyv視頻平台上傳的視頻id,把他放在一個數組裡面:

var arr = new Array("71da9e4b6985a7b5951dd62ce7bb020d_7", "71da9e4b691a41961dcd6524593d39ee_7", "71da9e4b69efa71be86d625508ec9feb_7"); 
然後利用迴圈,還有調用視頻播放結束介面s2j_onPlayOver( );跳轉到下一個要播放的視頻的id


視頻播放函數:printPlayer(index);



多終端控制碼如下,如果使用者是利用移動終端看的,(利用Html5)就會執行下面的程式碼片段:

if (navigator.userAgent.match(/iP(od|hone|ad)/i)) {var video = document.createElement("video");video.src = "http://v.polyv.net/uc/video/getMp4?vid="+vid;video.onended=s2j_onPlayOver;//video.addEventListener('ended',j2s_onPlayOver, false);video.autoplay="true";video.controls="controls"container.appendChild(video);}

如果使用者是利用pc端看的,就會執行下面的程式碼片段:

else{      var swf="http://player.polyv.net/player_polyv_20130926a.swf?vid="+vid;   container.innerHTML = "<OBJECT width='600' height='375' ><PARAM NAME=movie VALUE='"+vid+"'><param name='allowscriptaccess' value='always'>"   +"<param name='allowFullScreen' value='true' />"   +"<EMBED src='"+swf+"' width='600' height='375'  TYPE='application/x-shockwave-flash' allowscriptaccess='always' allowFullScreen='true' /></EMBED>"   +"</OBJECT>";  <!-- container.innerHTML = "<OBJECT>"+swf+"</OBJECT>";-->                    }

完整代碼:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="textml; charset=UTF-8"><title>Insert title here</title></head>       <p>視頻輪播11</p>   <div id="container"></div>       <p>視頻輪播22</p></body><ml><script type="text/javascript">var index=0;var arr = new Array("71da9e4b6985a7b5951dd62ce7bb020d_7", "71da9e4b691a41961dcd6524593d39ee_7", "71da9e4b69efa71be86d625508ec9feb_7"); var container = document.getElementById("container");function printPlayer(index){//alert(arr.length);if(index>arr.length-1){index=0;}//container.removeElement;container.innerHTML="";var vid=arr[index];if (navigator.userAgent.match(/iP(od|hone|ad)/i)) {var video = document.createElement("video");video.src = "http://v.polyv.net/uc/video/getMp4?vid="+vid;video.onended=s2j_onPlayOver;//video.addEventListener('ended',j2s_onPlayOver, false);video.autoplay="true";video.controls="controls"container.appendChild(video);}else{      var swf="http://player.polyv.net/player_polyv_20130926a.swf?vid="+vid;   container.innerHTML = "<OBJECT width='600' height='375' ><PARAM NAME=movie VALUE='"+vid+"'><param name='allowscriptaccess' value='always'>"   +"<param name='allowFullScreen' value='true' />"   +"<EMBED src='"+swf+"' width='600' height='375'  TYPE='application/x-shockwave-flash' allowscriptaccess='always' allowFullScreen='true' /></EMBED>"   +"</OBJECT>";  <!-- container.innerHTML = "<OBJECT>"+swf+"</OBJECT>";-->                    }}printPlayer(index);/* function j2s_onplayover(){index++;printPlayer(index);} */function s2j_onPlayOver() {alert('ssss');index++;printPlayer(index);}</script>


運行效果:

效果預覽地址1:http://www.polyv.net/

效果預覽地址2:http://forum.junjing.net/test3.php



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.