UNPKG

anghami-audio-manager

Version:
529 lines (527 loc) 29.5 kB
!function(a, b) { var c = this, d = c.document, e = "audio", f = "video", g = "script", h = function() { function a() { this.regex = { platform: { playstation: new RegExp("^Mozilla/[0-9].[0-9]\\s.(PlayStation [3-5]).*\\sSCEE/[1-9].[0-9]\\sNuanti/[1-9].[0-9]"), mobile: new RegExp("Mobile|Android|Tab|Tablet|GT|SM-(T|P)|Silk-Accelerated", "i"), tablet: new RegExp("Tab|Tablet|iPad|SM-(T|P)|GT|Silk-Accelerated", "i"), desktop: new RegExp("(?:(?!Mobile|Android|Tab|Tablet|GT|SM-(T|P)|Silk-Accelerated).)", "i") }, os: { android: new RegExp("Linux.*Android"), ios: new RegExp("like\\sMac\\sOS\\sX"), mac: new RegExp("Intel\\sMac\\sOS\\sX"), windows: new RegExp("Windows\\sNT") }, browser: { ie: new RegExp("msie|trident", "i"), edge: new RegExp("Edge"), chrome: new RegExp("Chrome"), firefox: new RegExp("Firefox"), safari: new RegExp("Safari") } }, this.canInit = !1, c.navigator && c.navigator.userAgent && c.RegExp && "function" == typeof c.RegExp ? this.canInit = !0 : this.throwErr(); } return a.prototype.detectDevice = function() { return { platform: this.detect("platform"), os: this.detect("os"), browser: this.detect("browser") }; }, a.prototype.detect = function(a) { if (this.canInit) { var b = this.regex[a], d = void 0; for (d in b) if (b[d].test(c.navigator.userAgent)) return d; } else this.throwErr(); }, a.prototype.throwErr = function() { if (c.Error && "function" == typeof c.Error) throw new Error("This browser doesn't supprot platform detection."); }, a; }(), i = function() { function a() { this.setup(); } return a.prototype.setup = function() { var a = this; this.audioElementEvents = { abort: function(b) { a.checkError(b); }, stalled: function(b) { a.checkError(b); }, suspend: function(b) { a.bufferProgress <= 98 && a.checkError(b); }, error: function(b) { if (a.checkError(b), a.options.video && a.isUsingHLSPolyfill) { var c = b.currentTarget.error; c.code === c.MEDIA_ERR_DECODE && a.hlsVideoTagErrorHandler(); } }, emptied: function(b) { a.audioTrackPlaying = void 0; }, waiting: function(b) { a.audioTrackPlaying = void 0, a.checkError(b), a.hasBuffer() && a.onBufferChange(b), a.dispatchEvent("wait", { status: a.audioTrackPlaying }); }, loadedmetadata: function(b) { a.metadata = !0; }, loadstart: function(b) { a.audioTrackPlaying = void 0, a.dispatchEvent("wait", { status: a.audioTrackPlaying }), a.hasBuffer() && a.onBufferChange(b); }, durationchange: function(b) { a.isValid() && a.onDurationChange(b), a.lastPosition && (a.setPosition(a.lastPosition), a.lastPosition = void 0), a.duration = b.target.duration; }, timeupdate: function(b) { a.playState = !0, a.isValid() && (a.onDurationChange(b), a.audioTrackPlaying = !0), a.hasBuffer() && a.onBufferChange(b), a.isReloading && (a.isReloading = a.error = !1), 100 === a.bufferProgress && a.clearWaiters(); }, canplay: function(b) { a.forcePause && !a.audioElement.paused && a.pause(void 0, !0), a.dispatchEvent("readyToPlay", { track: a.currentMediaItem, src: a.currentSrc, progress: a.progress }), a.options.playOnLoad && a.play(); }, playing: function(b) { a.forcePause && !a.audioElement.paused && a.pause(void 0, !0), a.playstation && (a.audioTrackPlaying = !0), a.isValid() && a.dispatchEvent("play", { status: a.audioTrackPlaying }), a.hasBuffer() && a.onBufferChange(b); }, pause: function(b) { a.forcePause = !1, a.isPaused = !0, a.audioTrackPlaying = !1, a.isValid() && a.dispatchEvent("pause", { status: a.audioTrackPlaying }); }, ended: function(b) { a.isValid() && a.dispatchEvent("end", { progress: a.progress }); }, progress: function(b) { a.hasBuffer() && a.onBufferChange(b); }, ratechange: function(b) { a.isValid() && a.dispatchEvent("rateChange"); }, volumechange: function(b) { a.isValid() && a.dispatchEvent("volumeChange"); }, seeking: function(b) { a.pause(); }, seeked: function(b) { a.options.playOnLoad && a.play(); } }, this.options = { html5: !0, reloadOnInit: 1e4, reloadOnError: 5e3, reloadCount: 5, volume: 1, fadeIn: !1, hls: !1, playOnLoad: !0 }, this.emptyVideoFileBase64 = "data:application/x-mpegURL;base64,UklGRjIAAABXQVZFZm10IBIAAAABAAEAQB8AAEAfAAABAAgAAABmYWN0BAAAAAAAAABkYXRhAAAAAA==", this.emptyWaveFileBase64 = "data:audio/wave;base64,UklGRjIAAABXQVZFZm10IBIAAAABAAEAQB8AAEAfAAABAAgAAABmYWN0BAAAAAAAAABkYXRhAAAAAA==", this.mimeRegx = new RegExp(/\.m4a|mp4|mpeg4|aac|flv|mov|m4v|f4v|.m4b|mp4v|3gp|3g2|mp4|m3u8/), this.eventMap = {}, this.list = this.playNextList = this.shuffleList = [], this.currentMediaItem = {}, this.online = !0, this.bufferProgress = this.progress = this.lastBufferProgress = this.position = this.remainingTime = this.currentIndex = this.shuffleIndex = this.duration = 0, this.audioElement = this.errorTimeout = this.lastPosition = this.error = this.metadata = this.isRepeating = this.isShuffling = this.isPaused = this.isReloading = this.interval = this.onlinecbreload = this.playState = this.forceDurationChangeForAudioAd = this.playPromise = void 0, this.device = new h().detectDevice(), this.windows = "ie" === this.device.browser, this.playstation = "playstation" === this.device.platform, this.loadEmpty = function() { return function() { a.forcePause = !1, a.load(!1); }; }(), this.windows || this.playstation || (c.addEventListener("offline", this.offlinecb, !1), c.addEventListener("online", this.onlinecb, !1)); }, a.prototype.appendHLSEvents = function() { var a = this; this.hls.on(c.Hls.Events.MANIFEST_PARSED, function() { a.options.playOnLoad && a.play(); }), this.hls.on(c.Hls.Events.ERROR, function(b, d) { if (d.fatal) switch (d.type) { case c.Hls.ErrorTypes.NETWORK_ERROR: a.hls.startLoad(); break; case c.Hls.ErrorTypes.MEDIA_ERROR: a.hlsjsErrorHandler(); break; default: a.destoryHLS(); } }); }, a.prototype.init = function(a) { return this.setOptions(a), this.createMedia(), this; }, a.prototype.isOnline = function() { var a = new XMLHttpRequest(); a.open("HEAD", "//" + c.location.hostname + "/?rand=" + c.Math.floor(65536 * (1 + c.Math.random())), !1); try { return a.send(), a.status >= 200 && (a.status < 300 || 304 === a.status); } catch (a) { return !1; } }, a.prototype.waitUntilOnline = function() { var a = this; return this.windows || this.playstation ? this.interval || (this.interval = c.setInterval(function() { a.online = a.isOnline(), a.online && a.reload(0); }, 2e3)) : this.onlinecbreload = !0, this; }, a.prototype.onlinecb = function() { this.online = !0, this.onlinecbreload && this.reload(0); }, a.prototype.offlinecb = function() { this.online = !1; }, a.prototype.onBufferChange = function(a) { try { var b = a.target.duration, d = a.target.buffered, e = a.target.currentTime, f = 0, g = d.length; if (this.lastBufferProgress = this.bufferProgress, b > 0 && g > 0) { for (;f < g; f++) if (d.start(g - 1 - f) < e) { this.bufferProgress = c.Math.ceil(d.end(g - 1 - f) / b * 100); break; } } else this.bufferProgress = void 0; this.isBufferValid() && this.dispatchEvent("bufferChange", { bufferProgress: this.bufferProgress }); } catch (a) {} }, a.prototype.onDurationChange = function(a) { var b = a.target.duration ? 1e3 * a.target.duration : 0, c = a.target.currentTime ? 1e3 * a.target.currentTime : 0, d = c / b * 100; this.setTimeValues(d, b - c, c); }, a.prototype.setTimeValues = function(a, b, c) { this.progress = a, this.remainingTime = b, this.position = c, this.position > 0 && this.dispatchEvent("durationChange", { progress: this.progress, remainingTime: this.remainingTime, position: this.position, duration: this.audioElement.duration, status: this.audioTrackPlaying }); }, a.prototype.setPosition = function(a) { if (this.playState) try { this.audioElement.currentTime = Number(a) / 1e3; } catch (a) {} return this; }, a.prototype.reload = function(a, b) { var d = this; this.position > 1e3 && (this.lastPosition = this.position), this.errorTimeout || (this.errorTimeout = c.setTimeout(function() { d.online && !d.audioTrackPlaying && d.error ? d.audioElement.readyState >= 2 && 2 === d.audioElement.networkState ? (d.isReloading = !0, d.clearWaiters().play(!0)) : d.load(d.currentSrc, !0) : (d.error = !1, d.isReloading = !1, d.clearWaiters()); }, a)); }, a.prototype.setVolume = function(a) { try { if (a > 1) return; return this.audioElement.volume = a, this.dispatchEvent("volumeChange", { volume: this.audioElement.volume }), this; } catch (a) {} }, a.prototype.getVolume = function() { return this.audioElement.volume; }, a.prototype.loadAudio = function() { return this.options.video && this.isUsingHLSPolyfill ? (this.createNewHlsContext(), this.hls.loadSource(this.currentSrc)) : this.audioElement.load(), this; }, a.prototype.load = function(a, b) { return this.flush(!0), b && this.loadAudio(), a ? this.setSrc(a) : this.setSrc(this.options.video ? this.emptyVideoFileBase64 : this.emptyWaveFileBase64), this.setAttribute("src", this.currentSrc), this.loadAudio(), this; }, a.prototype.fadeIn = function(a, b, d, e) { var f = this; b = Math.round(b); var g, h, i, j = b - a, k = null; this.setVolume(0); var l = function(m) { if (m || (m = new Date().getTime()), null === k && (k = m), g = m - k, h = g / d, f.setVolume(a + j * h), g < d && f.getVolume() < b) return void (i = c.requestAnimationFrame(l)); c.cancelAnimationFrame(i), e && e(), l = j = k = g = m = a = i = null; }; i = c.requestAnimationFrame(l); }, a.prototype.play = function() { this.isPaused = !1; try { this.checkFadeIn(), this.audioElement.play().catch(function(a) {}); } catch (a) {} return this; }, a.prototype.checkFadeIn = function() { this.options.fadeIn && this.fadeIn(0, .9, 5e3); }, a.prototype.pause = function(a, b) { if (a && (this.forcePause = !0), this.audioTrackPlaying || b) try { this.audioElement.pause(); } catch (a) {} return this; }, a.prototype.stop = function(a) { return this.pause().flush(!0).dispatchEvent("durationChange", { progress: 0, remainingTime: 0, position: 0 }), this.lastPosition = void 0, "function" == typeof a && a(), this; }, a.prototype.next = function(a, b) { return this[a] = this[a] === this.list.length - 1 ? 0 : ++this[a], this.currentMediaItem = this.list[this[a]], "function" == typeof b && b({ track: this.currentMediaItem }), this; }, a.prototype.previous = function(a, b) { return this[a] = this[a] <= 0 ? this.list.length - 1 : --this[a], this.currentMediaItem = this.list[this[a]], "function" == typeof b && b({ track: this.currentMediaItem }), this; }, a.prototype.repeat = function(a) { return this.isRepeating || this.toggle("repeat"), this.currentMediaItem = this.list[this.currentIndex], "function" == typeof a && a({ track: this.currentMediaItem }), this.dispatchEvent("repeatStatusChange", { repeat: this.isRepeating, track: this.currentMediaItem }), this; }, a.prototype.createShuffleList = function(a) { for (var b, c, d = a.length; 0 !== d; ) c = Math.floor(Math.random() * d), d -= 1, b = a[d], a[d] = a[c], a[c] = b; return a; }, a.prototype.shuffle = function(a) { return this.isShuffling || this.toggle("shuffle"), this.shuffleList = 0 === this.shuffleIndex ? this.createShuffleList(this.list) : this.shuffleList, "function" == typeof a && (this.currentMediaItem = this.shuffleList[this.shuffleIndex], a({ track: this.currentMediaItem, list: this.shuffleList })), this.dispatchEvent("shuffleStatusChange", { shuffle: this.isShuffling, track: this.currentMediaItem }), this; }, a.prototype.addToPlaylist = function(a, b, c) { var d; return "string" == typeof arguments[1] ? a.constructor === Array ? this[b] = d = a.length > 0 ? a : this[b] : (this[b].push(a), d = this[b]) : a.constructor === Array ? this.list = this.shuffleList = d = a.length > 0 ? a : this.list : (this.list.push(a), this.shuffleList = d = this.list), "function" == typeof c && c({ list: d }), this.dispatchEvent("playlistChange", { playlist: this.list, shuffleList: this.shuffleList }), this; }, a.prototype.clearPlaylist = function(a, b) { return this[a] = [], "function" == typeof b && b(), this.dispatchEvent("playlistChange", (c = {}, c[a] = this[a], c)), this; var c; }, a.prototype.setNextPlayingListItem = function(a) { return a.constructor === Array ? this.playNextList = this.playNextList.concat(a) : this.playNextList.push(a), this; }, a.prototype.getNextPlayingListItem = function() { return this.playNextList[this.playNextList.length - 1]; }, a.prototype.removeNextPlayingListItem = function() { return this.playNextList.splice(this.playNextList.length - 1, 1), this; }, a.prototype.hasNextPlayingListItem = function() { return this.playNextList.length > 0; }, a.prototype.applyNextAction = function(a) { var b = this; if (this.hasNextPlayingListItem()) { var c = function(c) { b.removeNextPlayingListItem(), b.currentMediaItem = c, a({ track: b.currentMediaItem }); }, d = this.getNextPlayingListItem(); this.isShuffling ? this.shuffleList.forEach(function(a) { a.id == d.id && c(a); }) : c(d); } else this.isRepeating ? this.repeat(a) : this.isShuffling ? this.shuffle().next("shuffleIndex", a) : this.next("currentIndex", a); return this; }, a.prototype.applyPreviousAction = function(a) { return this.isRepeating && this.toggle("repeat", !1), this.isShuffling ? this.shuffle().previous("shuffleIndex", a) : this.previous("currentIndex", a), this; }, a.prototype.toggle = function(a, b) { var c = "shuffle" === a ? "isShuffling" : "repeat" === a ? "isRepeating" : "", d = "shuffle" === a ? "isRepeating" : "repeat" === a ? "isShuffling" : ""; return this[c] = void 0 !== b ? b : !this[c], void 0 === b && (this[d] = !1), "shuffle" === c && (this.shuffleIndex = 0), this; }, a.prototype.flush = function(a) { return a && (this.isReloading = !0, this.clearWaiters()), this.options.video && this.isUsingHLSPolyfill && this.hls && this.destoryHLS(), this.bufferProgress = this.lastBufferProgress = this.progress = this.position = this.remainingTime = this.duration = 0, this.error = this.metadata = this.playState = void 0, this.currentMediaItem = {}, this.isUsingHLSPolyfill || this.setAttribute("src", this.emptyWaveFileBase64), this.setPosition(0), this; }, a.prototype.clearWaiters = function() { return c.clearTimeout(this.errorTimeout), this.errorTimeout = void 0, this.windows || this.playstation ? (c.clearInterval(this.interval), this.interval = void 0) : this.onlinecbreload = !1, this; }, a.prototype.setOptions = function(a) { if (a) { var b = void 0; for (b in a) this.options[b] = a[b], "volume" === b ? this.setVolume(a[b]) : "controls" === b ? this.audioElement.controls = a[b] : "poster" === b && this.setAttribute("poster", a[b]); } return this; }, a.prototype.createMedia = function() { var a = this; return this.options.vr ? (this.hasCustomScript = d.getElementById("gl-vr"), this.hasCustomScript ? this.createVRElement() : (this.script = d.createElement(g), this.script.src = "https://storage.googleapis.com/vrview/2.0/build/vrview.min.js", this.script.id = "gl-vr", this.script.async = !1, d.head.appendChild(this.script), this.script.onload = function() { a.createVRElement(), delete a.script; }), delete this.hasCustomScript) : this.options.html5 ? (this.audioElement = new Audio(), this.updateDomElement()) : this.options.hls ? (this.hasCustomScript = d.getElementById("gl-hls"), this.hasCustomScript ? this.createElement() : (this.script = d.createElement(g), this.script.src = "https://anghamiwebcdn.akamaized.net/web/vendor/hls.min.js", this.script.id = "gl-hls", this.script.async = !1, d.head.appendChild(this.script), this.script.onload = function() { a.createElement(), delete a.script; }), delete this.hasCustomScript) : this.createElement(), this; }, a.prototype.createNewHlsContext = function() { this.hls = new c.Hls({ autoStartLoad: !0, startPosition: -1, capLevelToPlayerSize: !0, debug: !1, defaultAudioCodec: void 0, initialLiveManifestSize: 1, maxBufferLength: 30, maxMaxBufferLength: 600, maxBufferSize: 6e7, maxBufferHole: .5, lowBufferWatchdogPeriod: .5, highBufferWatchdogPeriod: 3, nudgeOffset: .1, nudgeMaxRetry: 5, maxFragLookUpTolerance: .2, liveSyncDurationCount: 3, liveMaxLatencyDurationCount: 10, enableWorker: !0, enableSoftwareAES: !0, manifestLoadingTimeOut: 15e3, manifestLoadingMaxRetry: 4, manifestLoadingRetryDelay: 500, manifestLoadingMaxRetryTimeout: 64e3, startLevel: void 0, levelLoadingTimeOut: 1e4, levelLoadingMaxRetry: 4, levelLoadingRetryDelay: 500, levelLoadingMaxRetryTimeout: 64e3, fragLoadingTimeOut: 2e4, fragLoadingMaxRetry: 6, fragLoadingRetryDelay: 500, fragLoadingMaxRetryTimeout: 64e3, startFragPrefetch: !1, appendErrorMaxRetry: 3, enableWebVTT: !0, enableCEA708Captions: !0, stretchShortVideoTrack: !0, maxAudioFramesDrift: 1, forceKeyFrameOnDiscontinuity: !0, abrEwmaFastLive: 5, abrEwmaSlowLive: 9, abrEwmaFastVoD: 4, abrEwmaSlowVoD: 15, abrEwmaDefaultEstimate: 5e5, abrBandWidthFactor: .95, abrBandWidthUpFactor: .7, minAutoBitrate: 0 }), this.hls.attachMedia(this.audioElement), this.appendHLSEvents(); }, a.prototype.createVRElement = function() { new c.VRView.Player("#vrview", { video: this.options.vrSrc }).play(); }, a.prototype.createElement = function() { try { if (this.options.video) { var a = d.getElementById(f); this.audioElement = a.appendChild(d.createElement(f)), a = null, this.device.browser && this.options.hls && (this.isUsingHLSPolyfill = !0), this.updateDomElement(); } else this.audioElement = d.body.appendChild(d.createElement(e)), this.updateDomElement(); } catch (a) {} }, a.prototype.updateDomElement = function() { this.addEventListeners(), this.setAttribute("preload", "none"), this.options.video ? (this.audioElement.playsInline = !0, this.audioElement.setAttribute("webkit-playsinline", "true"), this.audioElement.style.width = "100%", this.audioElement.style.height = "100%", this.audioElement.style.display = "block", this.audioElement.style.position = "absolute", this.audioElement.style.top = "0", this.audioElement.style.left = "0", this.audioElement.style.right = "0", this.audioElement.style.bottom = "0", this.audioElement.style.backgroundColor = "black", this.setSrc(this.emptyVideoFileBase64).setAttribute("type", this.options.hls ? "application/x-mpegURL" : "video/mp4")) : this.setSrc(this.emptyWaveFileBase64).setAttribute("type", 'audio/mp4; codecs="mp4a.40.2"'), "function" == typeof this.options.onInit && this.options.onInit(); }, a.prototype.addEventListeners = function() { if (this.audioElement) { var a = void 0; for (a in this.audioElementEvents) this.audioElement.addEventListener(a, this.audioElementEvents[a], !1); this.options.video && this.isUsingHLSPolyfill && (this.hlsjsErrorHandler = this.hlsPolyfillVideoTagErrorHandlerFactory(), this.hlsVideoTagErrorHandler = this.hlsPolyfillVideoTagErrorHandlerFactory()); } return this; }, a.prototype.isValid = function() { return !!this.forceDurationChangeForAudioAd || null !== this.currentSrc.match(this.mimeRegx); }, a.prototype.isBufferValid = function() { return this.bufferProgress > 0 && this.bufferProgress !== this.lastBufferProgress; }, a.prototype.hasBuffer = function() { return !this.playstation && 100 !== this.bufferProgress; }, a.prototype.setSrc = function(a) { try { this.currentSrc = a; } catch (a) {} return this; }, a.prototype.setAttribute = function(a, b) { if (void 0 !== b) try { this.audioElement[a] = b; } catch (a) {} return this; }, a.prototype.removeAttribute = function(a) { try { this.audioElement.removeAttribute(a); } catch (a) {} return this; }, a.prototype.on = function(a, b) { this.eventMap[a] || (this.eventMap[a] = []), this.eventMap[a].push(b); }, a.prototype.off = function(a, b) { this.eventMap[a] = b; }, a.prototype.dispatchEvent = function(a, b) { var c = this.eventMap[a]; return c ? (c.forEach(function(a) { "function" == typeof a && a(b); }), this) : this; }, a.prototype.destroy = function() { var a; if (this.audioElement) for (a in this.audioElementEvents) this.audioElement.removeEventListener(a, this.audioElementEvents[a], !1); for (a in this.eventMap) delete this.eventMap[a]; return !this.options.html5 && this.audioElement && this.audioElement.parentNode.removeChild(this.audioElement), this.options.video && this.isUsingHLSPolyfill && (this.destoryHLS(), this.options = {}, this.isUsingHLSPolyfill = void 0, delete this.hlsjsErrorHandler, delete this.hlsVideoTagErrorHandler, delete this.hls), delete this.eventMap, this.audioElement && delete this.audioElement, this.eventMap = {}, this.audioElement = void 0, this; }, a.prototype.destoryHLS = function() { this.hls && (this.hls.destroy(), this.hls.detachMedia()); }, a.prototype.hlsPolyfillVideoTagErrorHandlerFactory = function() { var a = this, b = null, c = null; return function() { var d = Date.now(); !b || d - b > 2e3 ? (b = d, a.hls.recoverMediaError()) : !c || d - c > 2e3 ? (c = d, a.hls.swapAudioCodec(), a.hls.recoverMediaError()) : a.destoryHLS(); }; }, a.prototype.checkError = function(a) { this.isValid() && this.catchErrorVulnerability(a); }, a.prototype.showLoading = function() { this.audioTrackPlaying = void 0, this.dispatchEvent("wait", { status: this.audioTrackPlaying }); }, a.prototype.catchErrorVulnerability = function(a) { var b = this; if (100 !== this.bufferProgress) { var c = a.target.readyState, d = a.target.networkState, e = c >= 2 && this.metadata, f = 2 === d, g = a.target.error, h = g && g.code ? g.code : void 0, i = function(a) { b.showLoading(), b.error = !0, b.waitUntilOnline(); }, j = function(a) { b.showLoading(), b.error = !0, b.reload(a); }; if (this.online = this.windows || this.playstation ? this.isOnline() : this.online, this.online) { if (g && 4 === h) return this.showLoading(), void this.dispatchEvent("error", { progress: this.progress }); g && h > 1 || this.bufferProgress > 0 && !f && !e || this.playstation && this.playState ? j(this.options.reloadOnError) : (0 === this.bufferProgress || this.playstation && !this.playState) && (f && !e ? j(this.options.reloadOnInit + 1e4) : (!f && !e || e && !f) && j(this.options.reloadOnInit)); } else i(1e3); } }, a; }(); "object" == typeof module && module && "object" == typeof module.exports ? module.exports = i : "function" == typeof define && define.amd ? define(function() { return { constructor: i, instance: i }; }) : c.Galactic = i, b.true = a; }({}, function() { return this; }()); //# sourceMappingURL=AudioManager.min.js.map