Files
ArtPlayer/dist/artplayer.js
T
2018-10-12 17:16:49 +08:00

2 lines
12 KiB
JavaScript

!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t(e.artplayer={})}(this,function(e){"use strict";var o=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")};function r(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}var i=function(e,t,n){return t&&r(e.prototype,t),n&&r(e,n),e};function t(e,t){return e(t={exports:{}},t.exports),t.exports}var a=t(function(t){function n(e){return(n="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function r(e){return"function"==typeof Symbol&&"symbol"===n(Symbol.iterator)?t.exports=r=function(e){return n(e)}:t.exports=r=function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":n(e)},r(e)}t.exports=r});var s=function(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e};var u=function(e,t){return!t||"object"!==a(t)&&"function"!=typeof t?s(e):t},c=t(function(t){function n(e){return t.exports=n=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)},n(e)}t.exports=n}),f=t(function(n){function r(e,t){return n.exports=r=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e},r(e,t)}n.exports=r});var n=function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),t&&f(e,t)};var p=function(e){return-1!==Function.toString.call(e).indexOf("[native code]")},l=t(function(r){function o(e,t,n){return!function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],function(){})),!0}catch(e){return!1}}()?r.exports=o=function(e,t,n){var r=[null];r.push.apply(r,t);var o=new(Function.bind.apply(e,r));return n&&f(o,n.prototype),o}:r.exports=o=Reflect.construct,o.apply(null,arguments)}r.exports=o}),h=function(e){function r(e,t){var n;return o(this,r),n=u(this,c(r).call(this,e)),"function"==typeof Error.captureStackTrace&&Error.captureStackTrace(s(s(n)),t||n.constructor),n.name="ArtPlayerError",n}return n(r,e),r}(t(function(t){function r(e){var n="function"==typeof Map?new Map:void 0;return t.exports=r=function(e){if(null===e||!p(e))return e;if("function"!=typeof e)throw new TypeError("Super expression must either be null or a function");if(void 0!==n){if(n.has(e))return n.get(e);n.set(e,t)}function t(){return l(e,arguments,c(this).constructor)}return t.prototype=Object.create(e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),f(t,e)},r(e)}t.exports=r})(Error));function d(e,t){if(!e)throw new h(t)}function v(){}v.prototype={on:function(e,t,n){var r=this.e||(this.e={});return(r[e]||(r[e]=[])).push({fn:t,ctx:n}),this},once:function(e,t,n){var r=this;function o(){r.off(e,o),t.apply(n,arguments)}return o._=t,this.on(e,o,n)},emit:function(e){for(var t=[].slice.call(arguments,1),n=((this.e||(this.e={}))[e]||[]).slice(),r=0,o=n.length;r<o;r++)n[r].fn.apply(n[r].ctx,t);return this},off:function(e,t){var n=this.e||(this.e={}),r=n[e],o=[];if(r&&t)for(var i=0,a=r.length;i<a;i++)r[i].fn!==t&&r[i].fn._!==t&&o.push(r[i]);return o.length?n[e]=o:delete n[e],this}};var y=v,m=function(){function t(e){o(this,t),this.art=e,this.init()}return i(t,[{key:"init",value:function(){this.art.refs.$container.innerHTML='\n <div class="artplayer-wrap">\n <video class="artplayer-video" webkit-playsinline playsinline></video>\n <div class="artplayer-controls"></div>\n <div class="artplayer-layers"></div>\n <div class="artplayer-loading"></div>\n <div class="artplayer-notice"></div>\n </div>\n ',this.art.refs.$wrap=this.art.refs.$container.querySelector(".artplayer-wrap"),this.art.refs.$video=this.art.refs.$container.querySelector(".artplayer-video"),this.art.refs.$controls=this.art.refs.$container.querySelector(".artplayer-controls"),this.art.refs.$layers=this.art.refs.$container.querySelector(".artplayer-layers"),this.art.refs.$loading=this.art.refs.$container.querySelector(".artplayer-loading"),this.art.refs.$notice=this.art.refs.$container.querySelector(".artplayer-notice")}}]),t}(),b={},g={zh:b,"zh-cn":b,"zh-tw":{},en:{},jp:{}},S=function(){function n(e){var t=e.option;o(this,n),this.language=g[t.lang.toLowerCase()]||g.en}return i(n,[{key:"get",value:function(e){return this.language[e]||e}}]),n}(),E=["abort","canplay","canplaythrough","durationchange","emptied","ended","error","loadeddata","loadedmetadata","loadstart","pause","play","playing","progress","ratechange","seeked","seeking","stalled","suspend","timeupdate","volumechange","waiting"],w=function(){function t(e){o(this,t),this.art=e,this.eventFn=this.eventFn.bind(this),this.init(),this.eventBind()}return i(t,[{key:"init",value:function(){var e,t,n,r=this.art.option,o=this.art.refs.$video;o.controls=!0,o.poster=r.poster,o.volume=(e=r.volume,t=0,n=1,Math.max(Math.min(e,Math.max(t,n)),Math.min(t,n))),o.autoplay=r.autoplay,o.preload=r.preload}},{key:"eventBind",value:function(){for(var n=this,r=this.art.refs.$video,o=E,e=function(e){var t=o[e];r.addEventListener(t,n.eventFn),n.art.destroyEvents.push(function(){r.removeEventListener(t,n.eventFn)})},t=0;t<o.length;t++)e(t)}},{key:"eventFn",value:function(e){this.art.emit("video:".concat(e.type),e)}}]),t}(),B={mp4:'video/mp4; codecs="avc1.42E01E, mp4a.40.2"',webm:'video/webm; codecs="vorbis, vp8"',ts:'video/mp2t; codecs="avc1.42E01E, mp4a.40.2"'},k={propertys:["activeSourceBuffers","duration","readyState","sourceBuffers"],methods:["addSourceBuffer","endOfStream","removeSourceBuffer","clearLiveSeekableRange","setLiveSeekableRange"],events:["sourceclose","sourceended","sourceopen"]},x={propertys:["mode","updating","buffered","timestampOffset","audioTracks","videoTracks","textTracks","appendWindowStart","appendWindowEnd","trackDefaults"],methods:["appendBuffer","appendStream","abort","remove"],events:["abort","error","update","updateend","updatestart"]},F={propertys:["length"],events:["addsourcebuffer","removesourcebuffer"]},L=function(){function t(e){o(this,t),this.art=e,this.setMimeCodec(),this.mediaSourceEventFn=this.mediaSourceEventFn.bind(this),this.sourceBufferEventFn=this.sourceBufferEventFn.bind(this),this.sourceBuffersEventFn=this.sourceBuffersEventFn.bind(this),this.activeSourceBuffersEventFn=this.activeSourceBuffersEventFn.bind(this),this.init(),this.eventBind(),this.eventStart()}return i(t,[{key:"setMimeCodec",value:function(){var e=this.art.option;if(!e.type){var t=e.url.trim().toLowerCase().split("."),n=t[t.length-1];d(Object.keys(B).includes(n),"Can't find video's type from ".concat(e.url)),e.type=n}if(!e.mimeCodec){var r=B[e.type];d(r,"Can't find video's mimeCodec from ".concat(e.type)),e.mimeCodec=r}}},{key:"init",value:function(){var e=this.art,t=e.option,n=e.refs.$video;d(MediaSource.isTypeSupported(t.mimeCodec),"Unsupported MIME type or codec: ".concat(t.mimeCodec)),this.mediaSource=new MediaSource,n.src=URL.createObjectURL(this.mediaSource),this.art.destroyEvents.push(function(){URL.revokeObjectURL(n.src)})}},{key:"eventBind",value:function(){var t=this,e=F;k.events.forEach(function(e){t.mediaSource.addEventListener(e,t.mediaSourceEventFn),t.art.destroyEvents.push(function(){t.mediaSource.removeEventListener(e,t.mediaSourceEventFn)})}),e.events.forEach(function(e){t.mediaSource.sourceBuffers.addEventListener(e,t.sourceBuffersEventFn),t.art.destroyEvents.push(function(){t.mediaSource.sourceBuffers.removeEventListener(e,t.sourceBuffersEventFn)}),t.mediaSource.activeSourceBuffers.addEventListener(e,t.activeSourceBuffersEventFn),t.art.destroyEvents.push(function(){t.mediaSource.activeSourceBuffers.removeEventListener(e,t.activeSourceBuffersEventFn)})})}},{key:"eventStart",value:function(){var e,t=this,n=this.art.option,r=x;this.art.on("mediaSource:sourceopen",function(){t.sourceBuffer=t.mediaSource.addSourceBuffer(n.mimeCodec),r.events.forEach(function(e){t.sourceBuffer.addEventListener(e,t.sourceBufferEventFn),t.art.destroyEvents.push(function(){t.sourceBuffer.removeEventListener(e,t.sourceBufferEventFn)})})}),this.art.on("sourceBuffer:updateend",function(){t.mediaSource.endOfStream()}),(e=n.url,fetch(e).then(function(e){return e.arrayBuffer()}).catch(function(e){throw new h(e.message)})).then(function(e){t.sourceBuffer.appendBuffer(e)})}},{key:"mediaSourceEventFn",value:function(e){this.art.emit("mediaSource:".concat(e.type),e)}},{key:"sourceBufferEventFn",value:function(e){this.art.emit("sourceBuffer:".concat(e.type),e)}},{key:"sourceBuffersEventFn",value:function(e){this.art.emit("sourceBuffers:".concat(e.type),e)}},{key:"activeSourceBuffersEventFn",value:function(e){this.art.emit("activeSourceBuffers:".concat(e.type),e)}}]),t}(),O=function e(t){t.option;o(this,e)},q=function e(t){o(this,e),this.art=t},C=function e(t){o(this,e),this.art=t},$=function e(t){o(this,e),this.art=t},j=function e(t){o(this,e),this.art=t},M=function e(t){o(this,e),this.art=t},T=function e(t){o(this,e),this.art=t},R=function e(t){o(this,e),this.art=t},_=0,P=[],U=function(e){function r(e){var t,n;return o(this,r),(t=u(this,c(r).call(this))).option=Object.assign({},r.DEFAULTS,e),d((n=t.option).container,"'container' option is required."),d(n.url,"'url' option is required."),d(n.container instanceof Element,"'container' option require 'Element' type, but got '".concat(a(n.container),"'.")),d("string"==typeof n.url,"'url' option require 'string' type, but got '".concat(a(n.url),"'.")),d("string"==typeof n.poster,"'poster' option require 'string' type, but got '".concat(a(n.poster),"'.")),d("number"==typeof n.volume,"'volume' option require 'number' type, but got '".concat(a(n.volume),"'.")),d("boolean"==typeof n.autoplay,"'autoplay' option require 'boolean' type, but got '".concat(a(n.autoplay),"'.")),d(-1<["none","metadata","auto"].indexOf(n.preload),"'preload' option require one of 'none、metadata、auto'."),d("string"==typeof n.lang,"'lang' option require 'string' type, but got '".concat(a(n.lang),"'.")),d("string"==typeof n.type,"'type' option require 'string' type, but got '".concat(a(n.type),"'.")),d("string"==typeof n.mimeCodec,"'mimeCodec' option require 'string' type, but got '".concat(a(n.mimeCodec),"'.")),t.init(),t}return n(r,y),i(r,[{key:"init",value:function(){this.refs={$container:this.option.container},this.destroyEvents=[],this.template=new m(this),this.i18n=new S(this),this.player=new w(this),this.mse=new L(this),this.controls=new O(this),this.contextmenu=new q(this),this.danmaku=new C(this),this.subtitle=new j(this),this.info=new $(this),this.events=new M(this),this.hotkey=new T(this),this.layers=new R(this),this.id=_++,P.push(this)}},{key:"destroy",value:function(){this.destroyEvents.forEach(function(e){return e()}),this.refs.container.innerHTML="",P.splice(P.indexOf(this),1)}}],[{key:"use",value:function(e){var t=this.plugins||(this.plugins=[]);if(-1<t.indexOf(e))return this;var n=Array.from(arguments).slice(1);return n.unshift(this),"function"==typeof e.install?e.install.apply(e,n):"function"==typeof e&&e.apply(null,n),t.push(e),this}},{key:"version",get:function(){return"1.0.0"}},{key:"DEFAULTS",get:function(){return{container:document.querySelector(".artplayer"),url:"",poster:"",volume:.7,autoplay:!1,preload:"auto",type:"",mimeCodec:"",lang:navigator.language.toLowerCase()}}}]),r}();window.Artplayer=U,e.instances=P,e.default=U,Object.defineProperty(e,"__esModule",{value:!0})});