2025-02-17 21:36:37 +08:00
|
|
|
|
//AudioMgr.ts
|
|
|
|
|
import { Node, AudioClip, director } from 'cc';
|
|
|
|
|
import { AudioBgMusic } from './audio/AudioBgMusic';
|
|
|
|
|
import { AudioEffect } from './audio/AudioEffect';
|
|
|
|
|
import { ModuleDef } from '../../scripts/ModuleDef';
|
|
|
|
|
/**
|
|
|
|
|
* @en
|
|
|
|
|
* this is a sington class for audio play, can be easily called from anywhere in you project.
|
|
|
|
|
* @zh
|
|
|
|
|
* 这是一个用于播放音频的单件类,可以很方便地在项目的任何地方调用。
|
|
|
|
|
*/
|
|
|
|
|
export class AudioMgr {
|
|
|
|
|
|
|
|
|
|
private static _inst: AudioMgr;
|
|
|
|
|
public static get inst(): AudioMgr {
|
|
|
|
|
if (this._inst == null) {
|
|
|
|
|
this._inst = new AudioMgr();
|
|
|
|
|
}
|
|
|
|
|
return this._inst;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bgMusic!: AudioBgMusic;
|
|
|
|
|
|
|
|
|
|
private effect!: AudioEffect;
|
|
|
|
|
|
|
|
|
|
// private _audioSource: AudioSource;
|
|
|
|
|
private _musicVolume = 1.0;
|
|
|
|
|
private _musicVolumeScale = 1.0;
|
|
|
|
|
// private _soundVolume = 1.0;
|
|
|
|
|
constructor() {
|
|
|
|
|
//@en create a node as audioMgr
|
|
|
|
|
//@zh 创建一个节点作为 audioMgr
|
|
|
|
|
let audioMgr = new Node();
|
|
|
|
|
audioMgr.name = '__audioMgr__';
|
|
|
|
|
|
|
|
|
|
//@en add to the scene.
|
|
|
|
|
//@zh 添加节点到场景
|
|
|
|
|
director.getScene().addChild(audioMgr);
|
|
|
|
|
|
|
|
|
|
//@en make it as a persistent node, so it won't be destroied when scene change.
|
|
|
|
|
//@zh 标记为常驻节点,这样场景切换的时候就不会被销毁了
|
|
|
|
|
director.addPersistRootNode(audioMgr);
|
|
|
|
|
|
|
|
|
|
//@en add AudioSource componrnt to play audios.
|
|
|
|
|
//@zh 添加 AudioSource 组件,用于播放音频。
|
|
|
|
|
// this._audioSource = audioMgr.addComponent(AudioSource);
|
|
|
|
|
var bg_music = new Node("UIBgMusic");
|
|
|
|
|
bg_music.parent = audioMgr;
|
|
|
|
|
this.bgMusic = bg_music.addComponent(AudioBgMusic);
|
|
|
|
|
|
|
|
|
|
var effect = new Node("UIEffect");
|
|
|
|
|
effect.parent = audioMgr;
|
|
|
|
|
this.effect = effect.addComponent(AudioEffect);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// public get audioSource() {
|
|
|
|
|
// return this._audioSource;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
public set musicVolume(v: number) {
|
|
|
|
|
this._musicVolume = v;
|
|
|
|
|
this.bgMusic.volume = this._musicVolume * this._musicVolumeScale;
|
|
|
|
|
// this._audioSource.volume = this._musicVolume * this._musicVolumeScale;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public set soundVolume(v: number) {
|
|
|
|
|
// this._soundVolume = v;
|
|
|
|
|
this.effect.volume = v;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @en
|
|
|
|
|
* play short audio, such as strikes,explosions
|
|
|
|
|
* @zh
|
|
|
|
|
* 播放短音频,比如 打击音效,爆炸音效等
|
|
|
|
|
* @param sound clip or url for the audio
|
|
|
|
|
* @param volume
|
|
|
|
|
*/
|
|
|
|
|
playOneShot(sound: AudioClip | string, volume: number = 1.0, bundleName: string = 'resources') {
|
|
|
|
|
if (sound instanceof AudioClip) {
|
|
|
|
|
// this._audioSource.volume = 1.0;
|
|
|
|
|
// this._audioSource.playOneShot(sound, volume * this._soundVolume);
|
|
|
|
|
this.effect.playerEffect(sound, this.effect.volume);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
// this.effect.volume = volume;
|
|
|
|
|
this.effect.load(sound,bundleName);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @en
|
|
|
|
|
* play long audio, such as the bg music
|
|
|
|
|
* @zh
|
|
|
|
|
* 播放长音频,比如 背景音乐
|
|
|
|
|
* @param sound clip or url for the sound
|
|
|
|
|
* @param volume
|
|
|
|
|
* @param bundleName
|
|
|
|
|
*/
|
|
|
|
|
play(sound: AudioClip | string, volume: number = 1.0, bundleName: string = 'resources') {
|
|
|
|
|
this._musicVolumeScale = volume;
|
|
|
|
|
try {
|
|
|
|
|
if (sound instanceof AudioClip) {
|
|
|
|
|
this.bgMusic.playMusic(sound,null);
|
|
|
|
|
} else {
|
|
|
|
|
this.bgMusic.load(sound,null,bundleName);
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('An error occurred while playing the audio:', error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 播放通用按钮声音
|
|
|
|
|
*/
|
|
|
|
|
public playCommonBtn(res ?: string){
|
|
|
|
|
this.playOneShot(res || 'res/audio/camera',1.0,ModuleDef.BASIC);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* stop the audio play
|
|
|
|
|
*/
|
|
|
|
|
stop() {
|
|
|
|
|
// this._audioSource.stop();
|
|
|
|
|
this.effect.stop();
|
|
|
|
|
this.bgMusic.stop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* pause the audio play
|
|
|
|
|
*/
|
|
|
|
|
pause() {
|
|
|
|
|
// this._audioSource.pause();
|
|
|
|
|
this.effect.pause();
|
|
|
|
|
this.bgMusic.pause();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* resume the audio play
|
|
|
|
|
*/
|
|
|
|
|
resume() {
|
|
|
|
|
// this._audioSource.play();
|
|
|
|
|
this.effect.play();
|
|
|
|
|
this.bgMusic.play();
|
|
|
|
|
}
|
2025-02-07 10:49:34 +08:00
|
|
|
|
}
|