AudioManager.ts 3.1 KB
import { SettingEvents } from "../../event/BaseEvents";
import { GameBasicSettings } from "../settings/GameBasicSettings";
import { ResUtils } from "../../utils/ResUtils";
import { delay } from "simba-utils";

export namespace AudioManager {
    let _musicPathPrefix: string = "";
    let _voicePathPrefix: string = "";
    let _effectPathPrefix: string = "";

    export function init(musicPathPrefix = "", effectPathPrefix = "", voicePathPrefix = "") {
        _musicPathPrefix = musicPathPrefix;
        _voicePathPrefix = voicePathPrefix;
        _effectPathPrefix = effectPathPrefix;
        cc.audioEngine.setMusicVolume(GameBasicSettings.musicVolume);
        cc.audioEngine.setEffectsVolume(GameBasicSettings.soundVolume);
        SettingEvents.MusicVolumeChange.on((volume) => {
            cc.audioEngine.setMusicVolume(volume);
            if (volume === 0) {
                cc.audioEngine.pauseMusic();
            } else {
                cc.audioEngine.resumeMusic();
            }
        });
        SettingEvents.SoundVolumeChange.on((volume) => {
            cc.audioEngine.setEffectsVolume(volume);
        });
    }

    export async function playMusic(path: string) {
        if (path[0] !== "/") path = _musicPathPrefix + path;
        if (!GameBasicSettings.musicVolume) return;
        try {
            let clip = await ResUtils.loadRes<cc.AudioClip>(path, cc.AudioClip);
            cc.audioEngine.playMusic(clip, true);
        } catch (e) {
            console.error(e);
        }
    }

    export function pauseMusic() {
        cc.audioEngine.pauseMusic();
    }

    export function resumeMusic() {
        cc.audioEngine.resumeMusic();
    }

    export function stopMusic() {
        cc.audioEngine.stopMusic();
    }

    export async function playEffect(path: string, loopCount = 1) {
        if (path[0] !== "/") path = _effectPathPrefix + path;
        if (!GameBasicSettings.soundVolume) return NaN;
        try {
            let clip = await ResUtils.loadRes<cc.AudioClip>(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 delay(time);
                }
                return NaN;
            }
        } catch (e) {
            console.error(e);
        }
    }

    export async function playVoice(path: string) {
        if (path[0] !== "/") path = _voicePathPrefix + path;
        stopAllEffect();
        try {
            let clip = await ResUtils.loadRes<cc.AudioClip>(path, cc.AudioClip);
            let id = cc.audioEngine.playEffect(clip, false);
            let time = cc.audioEngine.getDuration(id);
            await delay(time);
        } catch (e) {
            console.error(e);
        }
    }

    export function stopEffect(id: number) {
        if (!isNaN(id)) {
            cc.audioEngine.stopEffect(id);
        }
    }

    export function stopAllEffect() {
        cc.audioEngine.stopAllEffects();
    }

}