CocosCreator3.x仿照2.x的AudioEngine

import { AudioClip, AudioSource, CCClass, Component, Node, _decorator } from "cc";

const { ccclass } = _decorator;

@ccclass('AudioEngine')
export class AudioEngine extends Component {
    public static Instance: AudioEngine;

    private _volume: number = 1;
    private _bgmName: string = "";

    onLoad() {
        if (AudioEngine.Instance == null) {
            AudioEngine.Instance = this;
        }
        else {
            this.destroy()
            return;
        }
    }

    public playMusic(clip: AudioClip, loop: boolean): string {
        return this.playAudio(clip, loop, true);
    }

    public playEffect(clip: AudioClip, loop: boolean): string {
        return this.playAudio(clip, loop, false);
    }

    public resumeBgm(): void {
        let as = this.getAudioSourceNode(this._bgmName);
        if (as && as.state == AudioSource.AudioState.PAUSED) {
            as.play();
        }
    }

    public resumeAll(): void {
        this.node.children.forEach(element => {
            let as = element.getComponent(AudioSource);
            if (as.state == AudioSource.AudioState.PAUSED) {
                as.play();
            }
        });
    }

    public pauseAll(): void {
        this.node.children.forEach(element => {
            let as = element.getComponent(AudioSource);
            as.pause();
        });
    }

    public stopMusic(): void {
        let as = this.getAudioSourceNode(this._bgmName);
        if (as) {
            as.stop();
        }
    }

    public stopEffect(name: string): void {
        this.stop(name);
    }

    public stop(name: string): void {
        let as = this.getAudioSourceNode(name);
        if (as) {
            as.stop();
        }
    }

    public stopAll(): void {
        this.node.children.forEach(element => {
            let as = element.getComponent(AudioSource);
            as.stop();
        });
    }

    public getVolume(name: string): number {
        let as = this.getAudioSourceNode(name);
        if (as) {
            return as.volume;
        }
        return 0;
    }

    public setVolume(name: string, volume): void {
        let as = this.getAudioSourceNode(name);
        if (as) {
            as.volume = volume;
        }
    }

    public setFinishCallback(name: string, func: Function): void {
        let as = this.getAudioSourceNode(name);
        if (as) {
            as.clip.once('ended', (err, data) => {
                if (func) {
                    func()
                }
            })
        }
    }

    public isPlaying(name: string): boolean {
        let as = this.getAudioSourceNode(name);
        if (as) {
            return as.playing;
        }
        else {
            return false;
        }
    }

    private playAudio(clip: AudioClip, loop: boolean, isBgm): string {
        let as: AudioSource = null
        if (isBgm) {
            if (this._bgmName != "") {
                as = this.getAudioSourceNode(this._bgmName);
            }
            this._bgmName = clip.name;
        }
        if (as == null) {
            as = this.getAudioSourceNode(clip.name);
        }
        if (as.playing) {
            as.pause();
        }
        as.clip = clip;
        as.loop = loop;
        as.volume = this._volume
        as.play();
        return clip.name;
    }

    private getAudioSourceNode(name: string): AudioSource {
        if (this.node != null) {
            let node = this.node.getChildByName(name);
            if (node) {
                return node.getComponent(AudioSource);
            }
            else {
                node = new Node();
                let as = node.addComponent(AudioSource);
                this.node.addChild(node);
                node.name = name;
                return as;
            }
        }
        return null;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值