Files
18xchat/assets/Scripts/Main/Manager/AudioManager.ts
T

134 lines
3.4 KiB
TypeScript
Raw Normal View History

2025-09-25 11:20:03 +08:00
import {
_decorator,
Node,
AudioClip,
AudioSource,
director,
assetManager,
} from "cc";
2025-07-17 17:18:21 +08:00
import PlayerDataManager from "./PlayerDataManager";
import ResManager from "./ResManager";
2025-09-21 20:05:36 +08:00
import { logger } from "db://assets/Scripts/Main/Common/Logger";
2025-09-25 11:20:03 +08:00
import { Config18x } from "../Config/Config18x";
2025-07-17 17:18:21 +08:00
const { ccclass, property } = _decorator;
//音频管理器
2025-09-25 11:20:03 +08:00
@ccclass("AudioManager")
2025-07-17 17:18:21 +08:00
export default class AudioManager {
2025-09-25 11:20:03 +08:00
private _audoSource: AudioSource;
private curBgId: string; //当前播放中的背景乐id 用于记录
private lastBgId: string; //上次播放的背景乐id 用于音乐开关切换时播放
private curBgId_confirm: string; //当前播放中的背景乐id 实际播放
2025-07-17 17:18:21 +08:00
2025-09-25 11:20:03 +08:00
private static _I: AudioManager = null;
public static get I(): AudioManager {
if (!AudioManager._I) {
AudioManager._I = new AudioManager();
AudioManager._I.init();
}
return AudioManager._I;
}
2025-07-17 17:18:21 +08:00
2025-09-25 11:20:03 +08:00
private init() {
let audioMgr = new Node();
audioMgr.name = "__audioMgr__";
director.getScene().addChild(audioMgr);
director.addPersistRootNode(audioMgr);
this._audoSource = audioMgr.addComponent(AudioSource);
}
2025-07-17 17:18:21 +08:00
2025-09-25 11:20:03 +08:00
/**播放音效 */
PlayEffect(sound: string, volume: number = 1.0) {
if (!sound) return;
if (!PlayerDataManager.I.IsSoundOn) {
return;
}
//let cfgs =
//if (!cfgs[sound]) return;
//let path = `Sound/${cfgs[sound].AssetName}`
logger.log("PlayEffect:", sound);
ResManager.I.getAudio(sound, (res: AudioClip) => {
this._audoSource.playOneShot(res, volume);
});
}
/**播放背景乐 */
PlayMusic(sound: string, loop: boolean = true, volume: number = 1.0) {
if (!sound || sound == this.curBgId_confirm) return;
//let cfgs = Resource.getConfig("Music")
//if (!cfgs[sound]) return;
this.lastBgId = sound;
if (!PlayerDataManager.I.IsMusicOn) {
return; //这个放在记录后面判断,防止开关打开时,没有记录
}
this.StopMusic();
this.curBgId = sound;
this.curBgId_confirm = sound;
//let path = `Music/${cfgs[sound].AssetName}`
logger.log("PlayMusic:", sound);
ResManager.I.getAudio(sound, (res: AudioClip) => {
this._audoSource.clip = res;
this._audoSource.play();
this._audoSource.volume = volume;
this._audoSource.loop = loop;
});
}
/**播放远程背景乐 */
PlayRemoteMusic(url, loop: boolean = true, volume: number = 1.0) {
logger.log(`下载远程背景乐: url= ${url}`);
assetManager.loadRemote<AudioClip>(
url,
{ ext: ".mp3" },
(err, res: AudioClip) => {
if (err) {
logger.log(err);
return;
2025-07-17 17:18:21 +08:00
}
2025-09-25 11:20:03 +08:00
logger.log(`播放远程背景乐: res= ${res}`);
this.StopMusic();
this._audoSource.clip = res;
this._audoSource.play();
this._audoSource.volume = volume;
this._audoSource.loop = loop;
}
);
}
/**停止播放背景乐 */
StopMusic() {
if (this.curBgId != null) {
this.lastBgId = this.curBgId;
2025-07-17 17:18:21 +08:00
}
2025-09-25 11:20:03 +08:00
this._audoSource.stop();
this.curBgId = null;
this.curBgId_confirm = null;
}
2025-07-17 17:18:21 +08:00
2025-09-25 11:20:03 +08:00
// 重新播放背景乐
ReplayMusic(): boolean {
if (this.curBgId != null) return false;
if (this.lastBgId == null) return false;
2025-07-17 17:18:21 +08:00
2025-09-25 11:20:03 +08:00
this.PlayMusic(this.lastBgId);
return true;
}
2025-07-17 17:18:21 +08:00
2025-09-25 11:20:03 +08:00
/**暂停播放背景乐 */
PauseMusic() {
this._audoSource.pause();
}
2025-07-17 17:18:21 +08:00
2025-09-25 11:20:03 +08:00
/**继续播放背景乐 */
ResumeMusic() {
this._audoSource.play();
}
}