ezuikit.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. /**
  2. * ezui 1.3
  3. */
  4. ( function( global, factory ) {
  5. "use strict";
  6. if ( typeof module === "object" && typeof module.exports === "object" ) {
  7. module.exports = global.document ?
  8. factory( global, true ) :
  9. function( w ) {
  10. if ( !w.document ) {
  11. throw new Error( "EZUIPlayer requires a window with a document" );
  12. }
  13. return factory( w );
  14. };
  15. } else {
  16. factory( global );
  17. }
  18. // Pass this if window is not defined yet
  19. } )( typeof window !== "undefined" ? window : this, function( window, noGlobal ){
  20. /**
  21. * @preserve HTML5 Shiv 3.7.3 | @afarkas @jdalton @jon_neal @rem | MIT/GPL2 Licensed
  22. */
  23. !function(a,b){function c(a,b){var c=a.createElement("p"),d=a.getElementsByTagName("head")[0]||a.documentElement;return c.innerHTML="x<style>"+b+"</style>",d.insertBefore(c.lastChild,d.firstChild)}function d(){var a=t.elements;return"string"==typeof a?a.split(" "):a}function e(a,b){var c=t.elements;"string"!=typeof c&&(c=c.join(" ")),"string"!=typeof a&&(a=a.join(" ")),t.elements=c+" "+a,j(b)}function f(a){var b=s[a[q]];return b||(b={},r++,a[q]=r,s[r]=b),b}function g(a,c,d){if(c||(c=b),l)return c.createElement(a);d||(d=f(c));var e;return e=d.cache[a]?d.cache[a].cloneNode():p.test(a)?(d.cache[a]=d.createElem(a)).cloneNode():d.createElem(a),!e.canHaveChildren||o.test(a)||e.tagUrn?e:d.frag.appendChild(e)}function h(a,c){if(a||(a=b),l)return a.createDocumentFragment();c=c||f(a);for(var e=c.frag.cloneNode(),g=0,h=d(),i=h.length;i>g;g++)e.createElement(h[g]);return e}function i(a,b){b.cache||(b.cache={},b.createElem=a.createElement,b.createFrag=a.createDocumentFragment,b.frag=b.createFrag()),a.createElement=function(c){return t.shivMethods?g(c,a,b):b.createElem(c)},a.createDocumentFragment=Function("h,f","return function(){var n=f.cloneNode(),c=n.createElement;h.shivMethods&&("+d().join().replace(/[\w\-:]+/g,function(a){return b.createElem(a),b.frag.createElement(a),'c("'+a+'")'})+");return n}")(t,b.frag)}function j(a){a||(a=b);var d=f(a);return!t.shivCSS||k||d.hasCSS||(d.hasCSS=!!c(a,"article,aside,dialog,figcaption,figure,footer,header,hgroup,main,nav,section{display:block}mark{background:#FF0;color:#000}template{display:none}")),l||i(a,d),a}var k,l,m="3.7.3",n=a.html5||{},o=/^<|^(?:button|map|select|textarea|object|iframe|option|optgroup)$/i,p=/^(?:a|b|code|div|fieldset|h1|h2|h3|h4|h5|h6|i|label|li|ol|p|q|span|strong|style|table|tbody|td|th|tr|ul)$/i,q="_html5shiv",r=0,s={};!function(){try{var a=b.createElement("a");a.innerHTML="<xyz></xyz>",k="hidden"in a,l=1==a.childNodes.length||function(){b.createElement("a");var a=b.createDocumentFragment();return"undefined"==typeof a.cloneNode||"undefined"==typeof a.createDocumentFragment||"undefined"==typeof a.createElement}()}catch(c){k=!0,l=!0}}();var t={elements:n.elements||"abbr article aside audio bdi canvas data datalist details dialog figcaption figure footer header hgroup main mark meter nav output picture progress section summary template time video",version:m,shivCSS:n.shivCSS!==!1,supportsUnknownElements:l,shivMethods:n.shivMethods!==!1,type:"default",shivDocument:j,createElement:g,createDocumentFragment:h,addElements:e};a.html5=t,j(b),"object"==typeof module&&module.exports&&(module.exports=t)}("undefined"!=typeof window?window:this,document);
  24. /*! @source http://purl.eligrey.com/github/classList.js/blob/master/classList.js */
  25. if("document" in self){if(!("classList" in document.createElement("_"))){(function(j){"use strict";if(!("Element" in j)){return}var a="classList",f="prototype",m=j.Element[f],b=Object,k=String[f].trim||function(){return this.replace(/^\s+|\s+$/g,"")},c=Array[f].indexOf||function(q){var p=0,o=this.length;for(;p<o;p++){if(p in this&&this[p]===q){return p}}return -1},n=function(o,p){this.name=o;this.code=DOMException[o];this.message=p},g=function(p,o){if(o===""){throw new n("SYNTAX_ERR","An invalid or illegal string was specified")}if(/\s/.test(o)){throw new n("INVALID_CHARACTER_ERR","String contains an invalid character")}return c.call(p,o)},d=function(s){var r=k.call(s.getAttribute("class")||""),q=r?r.split(/\s+/):[],p=0,o=q.length;for(;p<o;p++){this.push(q[p])}this._updateClassName=function(){s.setAttribute("class",this.toString())}},e=d[f]=[],i=function(){return new d(this)};n[f]=Error[f];e.item=function(o){return this[o]||null};e.contains=function(o){o+="";return g(this,o)!==-1};e.add=function(){var s=arguments,r=0,p=s.length,q,o=false;do{q=s[r]+"";if(g(this,q)===-1){this.push(q);o=true}}while(++r<p);if(o){this._updateClassName()}};e.remove=function(){var t=arguments,s=0,p=t.length,r,o=false,q;do{r=t[s]+"";q=g(this,r);while(q!==-1){this.splice(q,1);o=true;q=g(this,r)}}while(++s<p);if(o){this._updateClassName()}};e.toggle=function(p,q){p+="";var o=this.contains(p),r=o?q!==true&&"remove":q!==false&&"add";if(r){this[r](p)}if(q===true||q===false){return q}else{return !o}};e.toString=function(){return this.join(" ")};if(b.defineProperty){var l={get:i,enumerable:true,configurable:true};try{b.defineProperty(m,a,l)}catch(h){if(h.number===-2146823252){l.enumerable=false;b.defineProperty(m,a,l)}}}else{if(b[f].__defineGetter__){m.__defineGetter__(a,i)}}}(self))}else{(function(){var b=document.createElement("_");b.classList.add("c1","c2");if(!b.classList.contains("c2")){var c=function(e){var d=DOMTokenList.prototype[e];DOMTokenList.prototype[e]=function(h){var g,f=arguments.length;for(g=0;g<f;g++){h=arguments[g];d.call(this,h)}}};c("add");c("remove")}b.classList.toggle("c3",false);if(b.classList.contains("c3")){var a=DOMTokenList.prototype.toggle;DOMTokenList.prototype.toggle=function(d,e){if(1 in arguments&&!this.contains(d)===!e){return e}else{return a.call(this,d)}}}b=null}())}};
  26. Date.prototype.Format = function (fmt) { //author: meizz
  27. var o = {
  28. "M+": this.getMonth() + 1, //月份
  29. "d+": this.getDate(), //日
  30. "h+": this.getHours(), //小时
  31. "m+": this.getMinutes(), //分
  32. "s+": this.getSeconds(), //秒
  33. "q+": Math.floor((this.getMonth() + 3) / 3), //季度
  34. "S": this.getMilliseconds() //毫秒
  35. };
  36. if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  37. for (var k in o)
  38. if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  39. return fmt;
  40. };
  41. var Domain = 'https://open.ys7.com';
  42. var logDomain = 'https://log.ys7.com/statistics.do';
  43. var ckplayerJS = Domain + '/sdk/js/1.3/ckplayer/ckplayer.js';
  44. var ckplayerSWF = Domain + '/sdk/js/1.3/ckplayer/ckplayer.swf';
  45. var m3u8SWF = Domain + '/sdk/js/1.3/ckplayer/m3u8.swf';
  46. var hlsJS = Domain + '/sdk/js/1.3/hls.min.js';
  47. // 当前页面是否是https协议
  48. var isHttps = location.protocol === 'https:' ? true : false;
  49. // 是否为移动端
  50. var isMobile = !!navigator.userAgent.match(/(iPhone|iPod|iPad|Android|ios|SymbianOS)/i);
  51. var testVideo = document.createElement('video');
  52. // 是否支持video标签和addEventListener方法(主要为了区别ie8)
  53. var isModernBrowser = !!testVideo.canPlayType && !!window.addEventListener;
  54. // 是否能使用video原生播放hls
  55. var isNativeSupportHls = isModernBrowser && testVideo.canPlayType('application/vnd.apple.mpegURL');
  56. // 是否能使用hls.js播放
  57. var isSupportHls = false;
  58. // 是否使用flash
  59. var useFlash = false;
  60. // 本地信息上报
  61. var LOCALINFO = 'open_netstream_localinfo';
  62. // 预览主表上报
  63. var PLAY_MAIN = 'open_netstream_play_main';
  64. function dclog(obj){
  65. var logObj = {
  66. Ver: 'v.1.3.0',
  67. PlatAddr: 'open.ys7.com',
  68. ExterVer: 'Ez.1.3.0',
  69. CltType: 102,
  70. StartTime: (new Date()).Format('yyyy-MM-dd hh:mm:ss.S'), // 每个日志包含当前的时间
  71. OS: navigator.platform
  72. }
  73. for(var i in obj){
  74. logObj[i] = obj[i];
  75. }
  76. var tempArray = [];
  77. for(var j in logObj){
  78. tempArray.push(j + '=' + logObj[j]);
  79. }
  80. var params = '?' + tempArray.join('&');
  81. // 上报一次本地统计信息
  82. var img = new Image();
  83. img.src = logDomain + params;
  84. }
  85. // 上报一次本地信息
  86. dclog({
  87. systemName: LOCALINFO
  88. });
  89. var RTMP_REG = /^rtmp/;
  90. var HLS_REG = /\.m3u8/;
  91. // 获取元素样式
  92. function getStyle(el){
  93. return window.getComputedStyle
  94. ? window.getComputedStyle(el, null)
  95. : el.currentStyle;
  96. }
  97. // 加载js
  98. function addJs(filename, callback){
  99. var oJs = document.createElement("script");
  100. oJs.setAttribute("src", filename);
  101. oJs.onload = callback;
  102. document.getElementsByTagName("head")[0].appendChild(oJs);
  103. }
  104. var EZUIPlayer = function(videoId){
  105. if(!isModernBrowser){
  106. throw new Error('不支持ie8等低版本浏览器');
  107. return;
  108. }
  109. if(typeof videoId !== 'string'){
  110. throw new Error('EZUIPlayer requires parameter videoId');
  111. }
  112. this.videoId = videoId;
  113. this.video = document.getElementById(videoId);
  114. if(!this.video){
  115. throw new Error('EZUIPlayer requires parameter videoId');
  116. }
  117. this.opt = {};
  118. this.opt.sources = [];
  119. var sources = this.video.getElementsByTagName('source');
  120. // 转为数组对象,不受removeChild影响
  121. sources = Array.prototype.slice.call(sources, 0);
  122. if(this.video.src){
  123. // 移动端删除rtmp地址
  124. if(isMobile && RTMP_REG.test(this.video.src)){
  125. this.video.removeAttribute('src');
  126. this.video.load();
  127. }else{
  128. this.opt.sources.push(this.video.src);
  129. }
  130. }
  131. var l = sources.length;
  132. if(l > 0){
  133. for(var i = 0; i < l; i++){
  134. // 移动端删除rtmp地址
  135. if(isMobile && RTMP_REG.test(sources[i].src)){
  136. this.video.removeChild(sources[i]);
  137. }else{
  138. this.opt.sources.push(sources[i].src);
  139. }
  140. }
  141. }
  142. if(this.opt.sources.length < 1){
  143. throw new Error('no source found in video tag.');
  144. }
  145. this.opt.cur = 0;
  146. // 事件存储
  147. this.handlers = {};
  148. this.opt.poster = this.video.poster;
  149. var videoStyle = getStyle(this.video);
  150. var width = this.video.width;
  151. var height = this.video.height;
  152. if(width){
  153. this.opt.width = width;
  154. if(height){
  155. this.opt.height = height;
  156. }else{
  157. this.opt.height = 'auto';
  158. }
  159. this.log('video width:' + this.opt.width + ' height:' + this.opt.height);
  160. }else{
  161. this.opt.width = videoStyle.width;
  162. this.opt.height = videoStyle.height;
  163. this.log('videoStyle.width:' + videoStyle.width + ' wideoStyle.height:' + videoStyle.height);
  164. }
  165. this.opt.parentId = videoId;
  166. this.opt.autoplay = this.video.autoplay ? true : false;
  167. this.log('autoplay:' + this.video.autoplay);
  168. this.tryPlay();
  169. this.initTime = (new Date()).getTime();
  170. this.on('play', function(){
  171. // 上报播放成功信息
  172. dclog({
  173. systemName: PLAY_MAIN,
  174. playurl: this.opt.currentSource,
  175. Time: (new Date()).Format('yyyy-MM-dd hh:mm:ss.S'),
  176. Enc: 0, // 0 不加密 1 加密
  177. PlTp: 1, // 1 直播 2 回放
  178. Via: 2, // 2 服务端取流
  179. ErrCd: 0,
  180. Cost: (new Date()).getTime() - this.initTime // 毫秒数
  181. });
  182. });
  183. this.retry = 2;
  184. this.on('error', function(){
  185. dclog({
  186. systemName: PLAY_MAIN,
  187. playurl: this.opt.currentSource,
  188. cost: -1,
  189. ErrCd: -1
  190. });
  191. });
  192. };
  193. // 事件监听
  194. EZUIPlayer.prototype.on = function(eventName, callback){
  195. if(typeof eventName !== 'string' || typeof callback !== 'function'){
  196. return;
  197. }
  198. if(typeof this.handlers[eventName] === 'undefined'){
  199. this.handlers[eventName] = [];
  200. }
  201. this.handlers[eventName].push(callback);
  202. };
  203. // 事件触发
  204. EZUIPlayer.prototype.emit = function(){
  205. if(this.handlers[arguments[0]] instanceof Array){
  206. var handlers = this.handlers[arguments[0]];
  207. var l = handlers.length;
  208. for(var i = 0; i < l; i++){
  209. handlers[i].apply(this, Array.prototype.slice.call(arguments, 1));
  210. }
  211. }
  212. };
  213. // 尝试播放
  214. EZUIPlayer.prototype.tryPlay = function(){
  215. this.opt.currentSource = this.opt.sources[this.opt.cur];
  216. if(!this.opt.currentSource){
  217. this.log('未找到合适的播放URL');
  218. return;
  219. }
  220. var me = this;
  221. // 如果是HLS地址
  222. if(/\.m3u8/.test(this.opt.currentSource)){
  223. // 如果是手机浏览器环境,或者原生支持HLS播放的,直接使用video标签播放
  224. // 否则尝试使用hls.js播放,
  225. // 最后使用flash
  226. if(isMobile || isNativeSupportHls){
  227. this.log('使用原生video');
  228. this.video.style.heght = this.opt.height = Number(this.opt.width.replace(/px$/g, '')) * 9 / 16 + 'px';
  229. this.initVideoEvent();
  230. }else{
  231. if(isHttps){
  232. addJs(ckplayerJS, function(){
  233. me.initCKPlayer();
  234. });
  235. }else{
  236. addJs(hlsJS, function(){
  237. isSupportHls = Hls.isSupported();
  238. if(isSupportHls){
  239. me.log('使用hls.js');
  240. me.initHLS();
  241. }else{
  242. useFlash = true;
  243. me.log('2 使用flash');
  244. addJs(ckplayerJS, function(){
  245. me.initCKPlayer();
  246. });
  247. }
  248. });
  249. }
  250. }
  251. }else if(/^rtmp:/.test(this.opt.currentSource)){
  252. if(isMobile){
  253. this.opt.cur++;
  254. this.tryPlay();
  255. return;
  256. }else{
  257. addJs(ckplayerJS, function(){
  258. me.initCKPlayer();
  259. });
  260. }
  261. }
  262. };
  263. // 初始化hls.js
  264. EZUIPlayer.prototype.initHLS = function(){
  265. var me = this;
  266. var hls = new Hls({defaultAudioCodec: 'mp4a.40.2'}); // 萤石设备默认使用 AAC LC 音频编码
  267. hls.loadSource(this.opt.currentSource);
  268. hls.attachMedia(this.video);
  269. hls.on(Hls.Events.MANIFEST_PARSED, function(){
  270. if(me.opt.autoplay){
  271. me.video.play();
  272. }
  273. me.initVideoEvent();
  274. });
  275. hls.on(Hls.Events.ERROR, function (event, data) {
  276. if (data.fatal) {
  277. switch(data.type) {
  278. case Hls.ErrorTypes.NETWORK_ERROR:
  279. // try to recover network error
  280. console.log("fatal network error encountered, try to recover");
  281. hls.startLoad();
  282. break;
  283. case Hls.ErrorTypes.MEDIA_ERROR:
  284. console.log("fatal media error encountered, try to recover");
  285. hls.recoverMediaError();
  286. break;
  287. default:
  288. // cannot recover
  289. hls.destroy();
  290. break;
  291. }
  292. }
  293. });
  294. this.hls = hls;
  295. };
  296. // 日志
  297. EZUIPlayer.prototype.log = function(msg){
  298. this.emit('log', msg);
  299. };
  300. // 初始化ckplayer
  301. EZUIPlayer.prototype.initCKPlayer = function(){
  302. this.log('ckplayer初始化');
  303. var me = this;
  304. var events = {
  305. 'play': function(){me.emit('play')},
  306. 'pause': function(){me.emit('pause')},
  307. 'error': function(){me.emit('error')}
  308. };
  309. window.ckplayer_status = function(){
  310. me.log(arguments);
  311. events[arguments[0]] && events[arguments[0]]();
  312. };
  313. // 新增相同id的div标签,然后删除video标签
  314. this.videoFlash = document.createElement('DIV');
  315. this.video.parentNode.replaceChild(this.videoFlash, this.video);
  316. this.video = this.videoFlash;
  317. this.videoFlash.id = this.opt.parentId;
  318. var flashvars = null;
  319. // 如果rtmp服务器环境设置了视频暂停则断开链接
  320. // 需要修改ckplayer.js setup参数第30个值
  321. // 在播放暂停后点击播放是否采用重新链接的方式
  322. if(/^rtmp/.test(this.opt.currentSource)){
  323. flashvars = {
  324. f: this.opt.currentSource,
  325. c: 0,
  326. p: this.opt.autoplay ? 1 : 0,
  327. i: this.opt.poster,
  328. lv: 1,
  329. loaded: 'loadHandler'
  330. };
  331. }else if(/\.m3u8/.test(this.opt.currentSource)){
  332. flashvars = {
  333. s:4, // 4-使用swf视频流插件播放
  334. f:m3u8SWF,
  335. a: this.opt.currentSource,
  336. c: 0, // 0-使用ckplayer.js的配置 1-使用ckplayer.xml的配置
  337. lv:1, // 1-直播 0-普通方式
  338. p: this.opt.autoplay ? 1 : 0, // 1-默认播放 0-默认暂停
  339. i: this.opt.poster,
  340. loaded: 'loadHandler'
  341. };
  342. }else{
  343. flashvars = {
  344. f: this.opt.currentSource,
  345. c: 0,
  346. p: 1,
  347. loaded: 'loadHandler'
  348. };
  349. }
  350. var params={bgcolor:'#FFF',allowFullScreen:true,allowScriptAccess:'always',wmode:'transparent'};
  351. this.flashId = this.opt.parentId + 'flashId';
  352. CKobject.embedSWF(ckplayerSWF,this.opt.parentId,this.flashId,this.opt.width,this.opt.height,flashvars,params);
  353. };
  354. EZUIPlayer.prototype.initVideoEvent = function(){
  355. var me = this;
  356. var EVENT = {
  357. 'loadstart': function(e){
  358. me.log('loadstart...当浏览器开始查找音频/视频时...');
  359. me.emit('loadstart', e);
  360. },
  361. 'durationchange': function(e){
  362. me.log('durationchange...当音频/视频的时长已更改时...');
  363. me.emit('durationchange', e);
  364. },
  365. 'loadedmetadata': function(e){
  366. me.log('loadedmetadata...当浏览器已加载音频/视频的元数据时...');
  367. me.emit('loadedmetadata', e);
  368. },
  369. 'loadeddata': function(e){
  370. me.log('loadeddata...当浏览器已加载音频/视频的当前帧时...');
  371. me.emit('loadeddata', e);
  372. },
  373. 'progress': function(e){
  374. me.log('progress...当浏览器正在下载音频/视频时...');
  375. me.emit('progress', e);
  376. },
  377. 'canplay': function(e){
  378. me.log('canplay...当浏览器可以播放音频/视频时...');
  379. me.emit('canplay', e);
  380. },
  381. 'canplaythrough': function(e){
  382. me.log('canplaythrough...当浏览器可在不因缓冲而停顿的情况下进行播放时...');
  383. me.emit('canplaythrough', e);
  384. },
  385. 'abort': function(e){
  386. me.log('abort...当音频/视频的加载已放弃时...');
  387. me.emit('abort', e);
  388. },
  389. 'emptied': function(e){
  390. me.log('emptied...当目前的播放列表为空时...');
  391. me.emit('emptied', e);
  392. },
  393. 'ended': function(e){
  394. me.log('ended...当目前的播放列表已结束时...');
  395. me.emit('ended', e);
  396. },
  397. 'pause': function(e){
  398. me.log('pause...当音频/视频已暂停时...');
  399. me.emit('pause', e);
  400. },
  401. 'play': function(e){
  402. me.log('play...当音频/视频已开始或不再暂停时...');
  403. me.emit('play', e);
  404. },
  405. 'playing': function(e){
  406. me.log('playing...当音频/视频在已因缓冲而暂停或停止后已就绪时...');
  407. me.emit('playing', e);
  408. },
  409. 'ratechange': function(e){
  410. me.log('ratechange...当音频/视频的播放速度已更改时...');
  411. me.emit('ratechange', e);
  412. },
  413. 'seeked': function(e){
  414. me.log('seeked...当用户已移动/跳跃到音频/视频中的新位置时...');
  415. me.emit('seeked', e);
  416. },
  417. 'seeking': function(e){
  418. me.log('seeking...当用户开始移动/跳跃到音频/视频中的新位置时...');
  419. me.emit('seeking', e);
  420. },
  421. 'stalled': function(e){
  422. me.log('stalled...当浏览器尝试获取媒体数据,但数据不可用时...');
  423. me.emit('stalled', e);
  424. },
  425. 'suspend': function(e){
  426. me.log('suspend...当浏览器刻意不获取媒体数据时...');
  427. me.emit('suspend', e);
  428. if(me.opt.autoplay){
  429. me.video.play();
  430. }
  431. },
  432. 'timeupdate': function(e){
  433. //me.log('timeupdate...当目前的播放位置已更改时...');
  434. me.emit('timeupdate', e);
  435. },
  436. 'volumechange': function(e){
  437. me.log('volumechange...当音量已更改时...');
  438. me.emit('volumechange', e);
  439. },
  440. 'waiting': function(e){
  441. me.log('waiting...当视频由于需要缓冲下一帧而停止...');
  442. me.emit('waiting', e);
  443. },
  444. 'error': function(e){
  445. me.log('error...当在音频/视频加载期间发生错误时...');
  446. me.emit('error', e);
  447. }
  448. };
  449. for(var i in EVENT){
  450. this.video.addEventListener( i, EVENT[i], false);
  451. }
  452. ios11Hack(this.video);
  453. };
  454. EZUIPlayer.prototype.play = function(){
  455. this.opt.autoplay = true;
  456. if(!!window['CKobject']){
  457. CKobject.getObjectById(this.flashId).videoPlay();
  458. }else if(!!this.video){
  459. this.video.play();
  460. }
  461. };
  462. EZUIPlayer.prototype.pause = function(){
  463. this.opt.autoplay = false;
  464. if(!!window['CKobject']){
  465. CKobject.getObjectById(this.flashId).videoPause();
  466. }else if(!!this.video){
  467. this.video.pause();
  468. }
  469. };
  470. EZUIPlayer.prototype.stop = function () {
  471. // 执行停止
  472. this.opt.autoplay = false;
  473. if (!!window['CKobject']) {
  474. CKobject.getObjectById(this.flashId).videoClear();
  475. } else if (!!this.video) {
  476. if (!!this.hls) { // hls停止依赖this.hls
  477. // 通过暂停停止播放
  478. this.video.pause();
  479. // 停止取流
  480. this.hls.stopLoad();
  481. } else if (!!this.flv) {
  482. this.flv.pause();
  483. this.flv.unload();
  484. this.flv.detachMediaElement();
  485. this.flv.destroy();
  486. this.flv = null;
  487. } else if(!!this.JSmpeg) {
  488. this.JSmpeg.stop();
  489. // this.JSmpeg.destroy();
  490. }
  491. }
  492. };
  493. EZUIPlayer.prototype.load = function(){
  494. if(!!window['CKobject']){
  495. // flash load
  496. }else if(!!this.video){
  497. this.video.load();
  498. }
  499. };
  500. //
  501. // EZUIPlayer.prototype.remove = function(){
  502. //
  503. // };
  504. //
  505. // EZUIPlayer.prototype.clear = function(){
  506. //
  507. // };
  508. //
  509. // // 修改播放地址
  510. // EZUIPlayer.prototype.changeSource = function(source){
  511. //
  512. // };
  513. // iOS11手机HLS直播在m3u8响应时间过长后不继续请求的hack
  514. function ios11Hack(video){
  515. var isloadeddata = false;
  516. var isPlaying = false;
  517. var stalledCount = 0;
  518. video.addEventListener('loadeddata', function(){
  519. isloadeddata = true;
  520. }, false);
  521. video.addEventListener('stalled', function(){
  522. stalledCount++;
  523. if(!isPlaying){
  524. if(stalledCount >= 2 && !isloadeddata){
  525. video.load();
  526. video.play();
  527. isloadeddata = false;
  528. isPlaying = false;
  529. stalledCount = 0;
  530. }
  531. }
  532. }, false);
  533. video.addEventListener('playing', function(){
  534. isPlaying = true;
  535. });
  536. }
  537. if ( !noGlobal ) {
  538. window.EZUIPlayer = EZUIPlayer;
  539. }
  540. return EZUIPlayer;
  541. });