UNPKG

commonui-lib-test

Version:

"#common ui lib test"

136 lines (135 loc) 3.28 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const SoundEffects_1 = require("./SoundEffects"); const SoundBG_1 = require("./SoundBG"); /** * @api {class} SoundMgr 音效管理 * @apiName SoundMgr * @apiGroup sound * @apiDescription 音效管理 */ class SoundMgr { constructor() { } Init() { this._bgOn = true; this._effectOn = true; this._bgVolume = 1; this._effectVolume = 1; this._bg = new SoundBG_1.default(); this._bg.setVolume(this._bgVolume); this._effect = new SoundEffects_1.default(); this._effect.setVolume(this._effectVolume); Laya.SoundManager.autoStopMusic = false; } /** * 播放音效 * @param effectName */ playEffect(effectId) { if (!this._effectOn) return; this._effect.play(effectId); } /** * 播放背景音乐 * @param key */ playBg(bgName) { this._currBg = bgName; if (!this._bgOn) return; this._bg.play(bgName); } /** * 停止背景音乐 */ stopBg() { this._bg.stop(); } /** * 设置背景音乐是否开启 * @param $isOn */ setBgOn($isOn) { Laya.SoundManager.musicMuted = !$isOn; this._bgOn = $isOn; if (!this._bgOn) { this.stopBg(); } else { if (this._currBg) { this.playBg(this._currBg); } } } /** * 设置背景音乐音量 * @param volume */ setBgVolume(volume) { if (volume >= 1) { volume /= 100; } volume = Math.min(volume, 1); volume = Math.max(volume, 0); Laya.SoundManager.setMusicVolume(volume); this._bgVolume = volume; this._bg.setVolume(this._bgVolume); } /** * 获取背景音乐音量 * @returns {number} */ getBgVolume() { return this._bgVolume; } /** * 设置音效是否开启 * @param $isOn */ setEffectOn($isOn) { Laya.SoundManager.soundMuted = !$isOn; this._effectOn = $isOn; } /** * 设置音效音量 * @param volume */ setEffectVolume(volume) { if (volume >= 1) { volume /= 100; } volume = Math.min(volume, 1); volume = Math.max(volume, 0); Laya.SoundManager.setSoundVolume(volume); this._effectVolume = volume; this._effect.setVolume(this._effectVolume); } /** * 获取音效音量 * @returns {number} */ getEffectVolume() { return this._effectVolume; } get bgOn() { return this._bgOn; } get effectOn() { return this._effectOn; } static get Ins() { if (SoundMgr._instance == null) { SoundMgr._instance = new SoundMgr(); } return SoundMgr._instance; } destroy() { this._bg.stop(); this._effect.destroy(); this._bg.destroy(); this._currBg = ''; } } exports.default = SoundMgr;