UNPKG

commonui-lib-test

Version:

"#common ui lib test"

93 lines (92 loc) 2.89 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const SoundType_1 = require("./SoundType"); /** * @api {class} BaseSound 聲音載入,清除 * @apiName BaseSound * @apiGroup sound * @apiDescription 聲音載入,清除 */ class BaseSound { constructor() { this._clearTime = 3 * 60 * 1000; this.soundPath = ""; let self = this; self._cache = {}; self._loadingCache = new Array(); Laya.timer.loop(1 * 60 * 1000, self, self.dealSoundTimer); } /** * 处理音乐文件的清理 */ dealSoundTimer() { let self = this; let currTime = Laya.Browser.now(); let keys = Object.keys(self._cache); for (let i = 0, len = keys.length; i < len; i++) { let key = keys[i]; if (!self.checkCanClear(key)) continue; if (currTime - self._cache[key] >= self._clearTime) { delete self._cache[key]; Laya.loader.clearRes(key); } } } /** * 获取Sound * @param key * @returns {egret.Sound} */ getSound(key) { let self = this; self._key = key; self.soundPath = SoundType_1.default.Ins.ID[key]; let sound = Laya.loader.getRes(self.soundPath); if (sound) { if (self._cache[self.soundPath]) { self._cache[self.soundPath] = Laya.Browser.now(); } } else { if (self._loadingCache.indexOf(self.soundPath) != -1) { return sound; } self._loadingCache.push(self.soundPath); Laya.loader.load(self.soundPath, Laya.Handler.create(self, () => { let index = self._loadingCache.indexOf(self.soundPath); if (index != -1) { self._loadingCache.splice(index, 1); self._cache[self.soundPath] = Laya.Browser.now(); self.loadedPlay(self._key, self.soundPath); } }, null, false), null, Laya.Loader.SOUND); } return sound; } /** * 资源加载完成后处理播放,子类重写 * @param key */ loadedPlay(key, soundPath) { } /** * 检测一个文件是否要清除,子类重写 * @param key * @returns {boolean} */ checkCanClear(key) { return true; } destroy() { let self = this; Laya.timer.clear(self, self.dealSoundTimer); let keys = Object.keys(self._cache); for (let i = 0, len = keys.length; i < len; i++) { let key = keys[i]; delete self._cache[key]; Laya.loader.clearRes(key); } } } exports.default = BaseSound;