12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- // 音频管理类 单例模式
- // 用法:playSoundManager.getInstance().playSound("soundName",1,false);
- // 1.第一个参数是音频名称,2.第二个参数是音量,3.第三个参数是是否循环
- const { ccclass, property } = cc._decorator;
- @ccclass
- export default class PlaySoundManager extends cc.Component {
- // 单例
- private static _instance: PlaySoundManager = null;
- public static getInstance(): PlaySoundManager {
- if (!PlaySoundManager._instance) {
- PlaySoundManager._instance = new PlaySoundManager();
- }
- return PlaySoundManager._instance;
- }
- // 加载音频
- public loadSound(soundName: string, callback: Function): void {
- cc.loader.loadRes(
- soundName,
- cc.AudioClip,
- function (err, clip) {
- if (err) {
- cc.error(err.message || err);
- return;
- }
- if (callback) callback(clip);
- }.bind(this)
- );
- }
- /**
- * 播放音效
- *
- * @param {cc.AudioClip} soundClip
- * @param {boolean} [loop=true]
- * @param {number} volume
- * @memberof PlaySoundManager
- */
- public playSound(soundClip: cc.AudioClip, loop: boolean = true, volume?: number): void {
- cc.audioEngine.playEffect(soundClip, loop);
- }
- /**
- * 停止所有音效
- *
- * @memberof PlaySoundManager
- */
- public stopAllSound(): void {
- cc.audioEngine.stopAllEffects();
- }
- }
|