ea470a0a-d1f3-4de3-b067-5caa1940adcc.js 3.9 KB
"use strict";
cc._RF.push(module, 'ea470oK0fNN47BnXKoZQK3M', 'AudioManager');
// script/common/gameplay/managers/AudioManager.ts

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AudioManager = void 0;
const BaseEvents_1 = require("../../event/BaseEvents");
const GameBasicSettings_1 = require("../settings/GameBasicSettings");
const ResUtils_1 = require("../../utils/ResUtils");
const simba_utils_1 = require("simba-utils");
var AudioManager;
(function (AudioManager) {
    let _musicPathPrefix = "";
    let _voicePathPrefix = "";
    let _effectPathPrefix = "";
    function init(musicPathPrefix = "", effectPathPrefix = "", voicePathPrefix = "") {
        _musicPathPrefix = musicPathPrefix;
        _voicePathPrefix = voicePathPrefix;
        _effectPathPrefix = effectPathPrefix;
        cc.audioEngine.setMusicVolume(GameBasicSettings_1.GameBasicSettings.musicVolume);
        cc.audioEngine.setEffectsVolume(GameBasicSettings_1.GameBasicSettings.soundVolume);
        BaseEvents_1.SettingEvents.MusicVolumeChange.on((volume) => {
            cc.audioEngine.setMusicVolume(volume);
            if (volume === 0) {
                cc.audioEngine.pauseMusic();
            }
            else {
                cc.audioEngine.resumeMusic();
            }
        });
        BaseEvents_1.SettingEvents.SoundVolumeChange.on((volume) => {
            cc.audioEngine.setEffectsVolume(volume);
        });
    }
    AudioManager.init = init;
    async function playMusic(path) {
        if (path[0] !== "/")
            path = _musicPathPrefix + path;
        if (!GameBasicSettings_1.GameBasicSettings.musicVolume)
            return;
        try {
            let clip = await ResUtils_1.ResUtils.loadRes(path, cc.AudioClip);
            cc.audioEngine.playMusic(clip, true);
        }
        catch (e) {
            console.error(e);
        }
    }
    AudioManager.playMusic = playMusic;
    function pauseMusic() {
        cc.audioEngine.pauseMusic();
    }
    AudioManager.pauseMusic = pauseMusic;
    function resumeMusic() {
        cc.audioEngine.resumeMusic();
    }
    AudioManager.resumeMusic = resumeMusic;
    function stopMusic() {
        cc.audioEngine.stopMusic();
    }
    AudioManager.stopMusic = stopMusic;
    async function playEffect(path, loopCount = 1) {
        if (path[0] !== "/")
            path = _effectPathPrefix + path;
        if (!GameBasicSettings_1.GameBasicSettings.soundVolume)
            return NaN;
        try {
            let clip = await ResUtils_1.ResUtils.loadRes(path, cc.AudioClip);
            if (loopCount <= 0) {
                return cc.audioEngine.playEffect(clip, true);
            }
            else {
                for (let i = 0; i < loopCount; i++) {
                    let id = cc.audioEngine.playEffect(clip, false);
                    let time = cc.audioEngine.getDuration(id);
                    await simba_utils_1.delay(time);
                }
                return NaN;
            }
        }
        catch (e) {
            console.error(e);
        }
    }
    AudioManager.playEffect = playEffect;
    async function playVoice(path) {
        if (path[0] !== "/")
            path = _voicePathPrefix + path;
        stopAllEffect();
        try {
            let clip = await ResUtils_1.ResUtils.loadRes(path, cc.AudioClip);
            let id = cc.audioEngine.playEffect(clip, false);
            let time = cc.audioEngine.getDuration(id);
            await simba_utils_1.delay(time);
        }
        catch (e) {
            console.error(e);
        }
    }
    AudioManager.playVoice = playVoice;
    function stopEffect(id) {
        if (!isNaN(id)) {
            cc.audioEngine.stopEffect(id);
        }
    }
    AudioManager.stopEffect = stopEffect;
    function stopAllEffect() {
        cc.audioEngine.stopAllEffects();
    }
    AudioManager.stopAllEffect = stopAllEffect;
})(AudioManager = exports.AudioManager || (exports.AudioManager = {}));

cc._RF.pop();