(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : typeof define === 'function' && define.amd ? define(['exports'], factory) : (factory((global.artplayer = {}))); }(this, (function (exports) { 'use strict'; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var classCallCheck = _classCallCheck; function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } var createClass = _createClass; function createCommonjsModule(fn, module) { return module = { exports: {} }, fn(module, module.exports), module.exports; } var _typeof_1 = createCommonjsModule(function (module) { function _typeof2(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof2 = function _typeof2(obj) { return typeof obj; }; } else { _typeof2 = function _typeof2(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof2(obj); } function _typeof(obj) { if (typeof Symbol === "function" && _typeof2(Symbol.iterator) === "symbol") { module.exports = _typeof = function _typeof(obj) { return _typeof2(obj); }; } else { module.exports = _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : _typeof2(obj); }; } return _typeof(obj); } module.exports = _typeof; }); function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } var assertThisInitialized = _assertThisInitialized; function _possibleConstructorReturn(self, call) { if (call && (_typeof_1(call) === "object" || typeof call === "function")) { return call; } return assertThisInitialized(self); } var possibleConstructorReturn = _possibleConstructorReturn; var getPrototypeOf = createCommonjsModule(function (module) { function _getPrototypeOf(o) { module.exports = _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } module.exports = _getPrototypeOf; }); var setPrototypeOf = createCommonjsModule(function (module) { function _setPrototypeOf(o, p) { module.exports = _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } module.exports = _setPrototypeOf; }); function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) setPrototypeOf(subClass, superClass); } var inherits = _inherits; function _isNativeFunction(fn) { return Function.toString.call(fn).indexOf("[native code]") !== -1; } var isNativeFunction = _isNativeFunction; var construct = createCommonjsModule(function (module) { function isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } } function _construct(Parent, args, Class) { if (isNativeReflectConstruct()) { module.exports = _construct = Reflect.construct; } else { module.exports = _construct = function _construct(Parent, args, Class) { var a = [null]; a.push.apply(a, args); var Constructor = Function.bind.apply(Parent, a); var instance = new Constructor(); if (Class) setPrototypeOf(instance, Class.prototype); return instance; }; } return _construct.apply(null, arguments); } module.exports = _construct; }); var wrapNativeSuper = createCommonjsModule(function (module) { function _wrapNativeSuper(Class) { var _cache = typeof Map === "function" ? new Map() : undefined; module.exports = _wrapNativeSuper = function _wrapNativeSuper(Class) { if (Class === null || !isNativeFunction(Class)) return Class; if (typeof Class !== "function") { throw new TypeError("Super expression must either be null or a function"); } if (typeof _cache !== "undefined") { if (_cache.has(Class)) return _cache.get(Class); _cache.set(Class, Wrapper); } function Wrapper() { return construct(Class, arguments, getPrototypeOf(this).constructor); } Wrapper.prototype = Object.create(Class.prototype, { constructor: { value: Wrapper, enumerable: false, writable: true, configurable: true } }); return setPrototypeOf(Wrapper, Class); }; return _wrapNativeSuper(Class); } module.exports = _wrapNativeSuper; }); var ArtPlayerError = /*#__PURE__*/ function (_Error) { inherits(ArtPlayerError, _Error); function ArtPlayerError(message, context) { var _this; classCallCheck(this, ArtPlayerError); _this = possibleConstructorReturn(this, getPrototypeOf(ArtPlayerError).call(this, message)); if (typeof Error.captureStackTrace === 'function') { Error.captureStackTrace(assertThisInitialized(assertThisInitialized(_this)), context || _this.constructor); } _this.name = 'ArtPlayerError'; return _this; } return ArtPlayerError; }(wrapNativeSuper(Error)); function errorHandle(condition, msg) { if (!condition) { throw new ArtPlayerError(msg); } } function clamp(num, a, b) { return Math.max(Math.min(num, Math.max(a, b)), Math.min(a, b)); } function request(url) { return fetch(url).then(function (response) { return response.arrayBuffer(); }).catch(function (err) { throw new ArtPlayerError(err.message); }); } function getExt(url) { if (url.includes('?')) { return getExt(url.split('?')[0]); } return url.trim().toLowerCase().split('.').pop(); } function verification(option) { errorHandle(option.container, '\'container\' option is required.'); errorHandle(option.url, '\'url\' option is required.'); errorHandle(typeof option.container === 'string' || option.container instanceof Element, "'container' option require 'string' or 'Element' type, but got '".concat(_typeof_1(option.container), "'.")); errorHandle(typeof option.url === 'string', "'url' option require 'string' type, but got '".concat(_typeof_1(option.url), "'.")); errorHandle(typeof option.poster === 'string', "'poster' option require 'string' type, but got '".concat(_typeof_1(option.poster), "'.")); errorHandle(typeof option.volume === 'number', "'volume' option require 'number' type, but got '".concat(_typeof_1(option.volume), "'.")); errorHandle(typeof option.autoplay === 'boolean', "'autoplay' option require 'boolean' type, but got '".concat(_typeof_1(option.autoplay), "'.")); errorHandle(['none', 'metadata', 'auto'].indexOf(option.preload) > -1, '\'preload\' option require one of \'none、metadata、auto\'.'); errorHandle(typeof option.lang === 'string', "'lang' option require 'string' type, but got '".concat(_typeof_1(option.lang), "'.")); errorHandle(typeof option.type === 'string', "'type' option require 'string' type, but got '".concat(_typeof_1(option.type), "'.")); errorHandle(typeof option.mimeCodec === 'string', "'mimeCodec' option require 'string' type, but got '".concat(_typeof_1(option.mimeCodec), "'.")); errorHandle(Array.isArray(option.layers), "'layers' option require 'array' type, but got '".concat(_typeof_1(option.layers), "'.")); errorHandle(Array.isArray(option.contextmenu), "'contextmenu' option require 'array' type, but got '".concat(_typeof_1(option.contextmenu), "'.")); errorHandle(typeof option.loading === 'string' || option.loading instanceof Element, "'loading' option require 'string' type, but got '".concat(_typeof_1(option.loading), "'.")); errorHandle(typeof option.theme === 'string', "'theme' option require 'string' type, but got '".concat(_typeof_1(option.theme), "'.")); errorHandle(typeof option.hotkey === 'boolean', "'hotkey' option require 'boolean' type, but got '".concat(_typeof_1(option.hotkey), "'.")); errorHandle(typeof option.subtitle === 'string', "'subtitle' option require 'string' type, but got '".concat(_typeof_1(option.subtitle), "'.")); errorHandle(Object.prototype.toString.call(option.subtitleStyle) === '[object Object]', "'subtitleStyle' option require 'object' type, but got '".concat(_typeof_1(option.subtitleStyle), "'.")); } var mimeCodec = { mp4: 'video/mp4; codecs="avc1.42E01E, mp4a.40.2"', webm: 'video/webm; codecs="vorbis, vp8"', ts: 'video/mp2t; codecs="avc1.42E01E, mp4a.40.2"' }; var mse = { mediaSource: { propertys: ['activeSourceBuffers', 'duration', 'readyState', 'sourceBuffers'], methods: ['addSourceBuffer', 'endOfStream', 'removeSourceBuffer', 'clearLiveSeekableRange', 'setLiveSeekableRange'], events: ['sourceclose', 'sourceended', 'sourceopen'] }, sourceBuffer: { propertys: ['mode', 'updating', 'buffered', 'timestampOffset', 'audioTracks', 'videoTracks', 'textTracks', 'appendWindowStart', 'appendWindowEnd', 'trackDefaults'], methods: ['appendBuffer', 'appendStream', 'abort', 'remove'], events: ['abort', 'error', 'update', 'updateend', 'updatestart'] }, sourceBufferList: { propertys: ['length'], events: ['addsourcebuffer', 'removesourcebuffer'] } }; var video = { propertys: ['audioTracks', 'autoplay', 'buffered', 'controller', 'controls', 'crossOrigin', 'currentSrc', 'currentTime', 'defaultMuted', 'defaultPlaybackRate', 'duration', 'ended', 'error', 'loop', 'mediaGroup', 'muted', 'networkState', 'paused', 'playbackRate', 'played', 'preload', 'readyState', 'seekable', 'seeking', 'src', 'startDate', 'textTracks', 'videoTracks', 'volume'], methods: ['addTextTrack', 'canPlayType', 'load', 'play', 'pause'], events: ['abort', 'canplay', 'canplaythrough', 'durationchange', 'emptied', 'ended', 'error', 'loadeddata', 'loadedmetadata', 'loadstart', 'pause', 'play', 'playing', 'progress', 'ratechange', 'seeked', 'seeking', 'stalled', 'suspend', 'timeupdate', 'volumechange', 'waiting'] }; var config = { mimeCodec: mimeCodec, mse: mse, video: video }; function E () { // Keep this empty so it's easier to inherit from // (via https://github.com/lipsmack from https://github.com/scottcorgan/tiny-emitter/issues/3) } E.prototype = { on: function (name, callback, ctx) { var e = this.e || (this.e = {}); (e[name] || (e[name] = [])).push({ fn: callback, ctx: ctx }); return this; }, once: function (name, callback, ctx) { var self = this; function listener () { self.off(name, listener); callback.apply(ctx, arguments); } listener._ = callback; return this.on(name, listener, ctx); }, emit: function (name) { var data = [].slice.call(arguments, 1); var evtArr = ((this.e || (this.e = {}))[name] || []).slice(); var i = 0; var len = evtArr.length; for (i; i < len; i++) { evtArr[i].fn.apply(evtArr[i].ctx, data); } return this; }, off: function (name, callback) { var e = this.e || (this.e = {}); var evts = e[name]; var liveEvents = []; if (evts && callback) { for (var i = 0, len = evts.length; i < len; i++) { if (evts[i].fn !== callback && evts[i].fn._ !== callback) liveEvents.push(evts[i]); } } // Remove event from queue to prevent memory leak // Suggested by https://github.com/lazd // Ref: https://github.com/scottcorgan/tiny-emitter/commit/c6ebfaa9bc973b33d110a84a307742b7cf94c953#commitcomment-5024910 (liveEvents.length) ? e[name] = liveEvents : delete e[name]; return this; } }; var tinyEmitter = E; var Template = /*#__PURE__*/ function () { function Template(art) { classCallCheck(this, Template); this.art = art; this.init(); } createClass(Template, [{ key: "init", value: function init() { var refs = this.art.refs; refs.$container.innerHTML = "\n
".concat(item, "
"); }).join(''); } }); } } }, { key: "show", value: function show() { this.art.refs.$subtitle.style.display = 'block'; } }, { key: "hide", value: function hide() { this.art.refs.$subtitle.style.display = 'none'; } }, { key: "change", value: function change(url) { errorHandle(getExt(url) === 'vtt', "'url' option require 'vtt' format, but got '".concat(getExt(url), "'.")); this.art.refs.$track.src = url; } }]); return Subtitle; }(); var Events = /*#__PURE__*/ function () { function Events(art) { classCallCheck(this, Events); this.art = art; this.destroyEvents = []; this.proxy = this.proxy.bind(this); } createClass(Events, [{ key: "proxy", value: function proxy(target, name, callback) { var option = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {}; target.addEventListener(name, callback, option); this.destroyEvents.push(function () { target.removeEventListener(name, callback, option); }); } }, { key: "destroy", value: function destroy() { this.destroyEvents.forEach(function (event) { return event(); }); } }]); return Events; }(); var Hotkey = /*#__PURE__*/ function () { function Hotkey(art) { classCallCheck(this, Hotkey); this.art = art; if (this.art.option.hotkey) { this.init(); } } createClass(Hotkey, [{ key: "init", value: function init() { var _this = this; var _this$art = this.art, player = _this$art.player, notice = _this$art.notice, i18n = _this$art.i18n, $container = _this$art.refs.$container, proxy = _this$art.events.proxy; proxy(document, 'click', function (event) { _this.art.focus = event.path.indexOf($container) > -1; }); proxy(window, 'keydown', function (event) { if (_this.art.focus) { var tag = document.activeElement.tagName.toUpperCase(); var editable = document.activeElement.getAttribute('contenteditable'); if (tag !== 'INPUT' && tag !== 'TEXTAREA' && editable !== '' && editable !== 'true') { var percentage; switch (event.keyCode) { case 39: event.preventDefault(); player.seek(player.currentTime() + 10); notice.show(i18n.get('Fast forward 10 seconds'), true); break; case 37: event.preventDefault(); player.seek(player.currentTime() - 10); notice.show(i18n.get('Rewind 10 seconds'), true); break; case 38: event.preventDefault(); percentage = player.volume() + 0.1; player.volume(percentage); notice.show(i18n.get('10% increase in volume'), true); break; case 40: event.preventDefault(); percentage = player.volume() - 0.1; player.volume(percentage); notice.show(i18n.get('10% reduction in volume'), true); break; case 32: event.preventDefault(); player.toggle(); break; default: break; } } } }); } }]); return Hotkey; }(); var id = 0; var Layers = /*#__PURE__*/ function () { function Layers(art) { classCallCheck(this, Layers); this.art = art; this.add = this.add.bind(this); this.art.option.layers.forEach(this.add); this.init(); } createClass(Layers, [{ key: "init", value: function init() { var _this$art = this.art, refs = _this$art.refs, player = _this$art.player, proxy = _this$art.events.proxy; proxy(refs.$layers, 'click', function (event) { if (event.path[0] === refs.$layers) { player.pause(); } }); } }, { key: "add", value: function add(option, callback) { var refs = this.art.refs; id++; var $layer = document.createElement('div'); $layer.setAttribute('data-art-layer-id', id); $layer.setAttribute('class', "art-layer art-layer-".concat(option.name || id)); $layer.style.zIndex = option.index || id; if (option.html instanceof Element) { $layer.appendChild(option.html); } else { $layer.innerHTML = option.html; } if (option.style) { Object.keys(option.style).forEach(function (key) { $layer.style[key] = option.style[key]; }); } refs.$layers.appendChild($layer); callback && callback($layer); } }, { key: "show", value: function show() { this.art.refs.$layers.style.display = 'block'; } }, { key: "hide", value: function hide() { this.art.refs.$layers.style.display = 'none'; } }]); return Layers; }(); var loading = "\n"; var play = "\n"; var Icons = { loading: loading, play: play }; function creatDomFromSvg(map) { var result = {}; Object.keys(map).forEach(function (name) { var tmp = document.createElement('div'); tmp.innerHTML = "").concat(map[name], ""); var _tmp$childNodes = slicedToArray(tmp.childNodes, 1); result[name] = _tmp$childNodes[0]; }); return result; } var Icons$1 = creatDomFromSvg(Icons); var Loading = /*#__PURE__*/ function () { function Loading(art) { classCallCheck(this, Loading); this.art = art; this.init(); } createClass(Loading, [{ key: "init", value: function init() { var _this$art = this.art, option = _this$art.option, $loading = _this$art.refs.$loading; if (option.loading) { if (option.loading instanceof Element) { $loading.appendChild(option.loading); } else { $loading.innerHTML = option.loading; } } else { $loading.appendChild(Icons$1.loading); } } }, { key: "hide", value: function hide() { this.art.refs.$loading.style.display = 'none'; } }, { key: "show", value: function show() { this.art.refs.$loading.style.display = 'flex'; } }]); return Loading; }(); var Notice = /*#__PURE__*/ function () { function Notice(art) { classCallCheck(this, Notice); this.art = art; this.timer = null; } createClass(Notice, [{ key: "show", value: function show(msg, autoHide) { var _this = this; var $notice = this.art.refs.$notice; $notice.style.display = 'block'; $notice.innerHTML = msg instanceof Error ? msg.message.trim() : msg; if (autoHide) { clearTimeout(this.timer); this.timer = setTimeout(function () { _this.hide(); }, 1000); } } }, { key: "hide", value: function hide() { var $notice = this.art.refs.$notice; $notice.style.display = 'none'; } }]); return Notice; }(); var Mask = /*#__PURE__*/ function () { function Mask(art) { classCallCheck(this, Mask); this.art = art; this.init(); } createClass(Mask, [{ key: "init", value: function init() { var _this = this; var _this$art = this.art, player = _this$art.player, option = _this$art.option, refs = _this$art.refs, proxy = _this$art.events.proxy; Icons$1.play.style.backgroundColor = option.theme; refs.$mask.appendChild(Icons$1.play); proxy(refs.$mask, 'click', function () { player.play(); _this.hide(); }); } }, { key: "show", value: function show() { this.art.refs.$mask.style.display = 'flex'; } }, { key: "hide", value: function hide() { this.art.refs.$mask.style.display = 'none'; } }]); return Mask; }(); var id$1 = 0; var instances = []; var Artplayer = /*#__PURE__*/ function (_Emitter) { inherits(Artplayer, _Emitter); function Artplayer(option) { var _this; classCallCheck(this, Artplayer); _this = possibleConstructorReturn(this, getPrototypeOf(Artplayer).call(this)); _this.option = Object.assign({}, Artplayer.DEFAULTS, option); verification(_this.option); _this.init(); return _this; } createClass(Artplayer, [{ key: "init", value: function init() { this.focus = false; this.playing = false; this.refs = {}; if (this.option.container instanceof Element) { this.refs.$container = this.option.container; } else { this.refs.$container = document.querySelector(this.option.container); } this.template = new Template(this); this.i18n = new I18n(this); this.events = new Events(this); this.player = new Player(this); this.mse = new Mse(this); this.layers = new Layers(this); this.controls = new Controls(this); this.contextmenu = new Contextmenu(this); this.danmu = new Danmu(this); this.subtitle = new Subtitle(this); this.info = new Info(this); this.loading = new Loading(this); this.notice = new Notice(this); this.hotkey = new Hotkey(this); this.mask = new Mask(this); this.id = id$1++; instances.push(this); return this; } }, { key: "destroy", value: function destroy() { this.events.destroy(); this.refs.$container.innerHTML = ''; instances.splice(instances.indexOf(this), 1); } }], [{ key: "use", value: function use(plugin) { var installedPlugins = this.plugins || (this.plugins = []); if (installedPlugins.indexOf(plugin) > -1) { return this; } var args = Array.from(arguments).slice(1); args.unshift(this); if (typeof plugin.install === 'function') { plugin.install.apply(plugin, args); } else if (typeof plugin === 'function') { plugin.apply(null, args); } installedPlugins.push(plugin); return this; } }, { key: "version", get: function get() { return '1.0.0'; } }, { key: "config", get: function get() { return config; } }, { key: "DEFAULTS", get: function get() { return { container: document.querySelector('.artplayer'), url: '', poster: '', volume: 0.7, autoplay: false, preload: 'auto', type: '', mimeCodec: '', layers: [], contextmenu: [], loading: '', theme: '#1aafff', hotkey: true, subtitle: '', subtitleStyle: {}, lang: navigator.language.toLowerCase() }; } }]); return Artplayer; }(tinyEmitter); window.Artplayer = Artplayer; exports.instances = instances; exports.default = Artplayer; Object.defineProperty(exports, '__esModule', { value: true }); }))); //# sourceMappingURL=artplayer.js.map