JM_KA/assets/Script/Engine/Utils/Audio/CSCommonAudios.ts
2022-08-26 16:48:17 +08:00

168 lines
5.3 KiB
TypeScript

import CSAudio from "./CSAudio";
export default class CSAppAudios {
private static _instanceApp: CSAppAudios = null;
public static get InstanceApp(): CSAppAudios { return this._instanceApp; }
private _idToAppClip: Map<number, cc.AudioClip> = new Map<number, cc.AudioClip>();
private _idToAppPath: Map<number, string> = new Map<number, string>();
private _idToAppAudioId: Map<number, number> = new Map<number, number>();
private _currentAppMusic: number = -1;
/** 判斷音效播放太過頻繁不疊加 */
private _lastAppPlaySoundTime: Map<string, number> = new Map<string, number>();
private readonly _appCanPlaySoundCutTime: number = 10;
constructor() {
CSAppAudios._instanceApp = this;
}
public AddAppClipsInfo(clips: Map<number, cc.AudioClip>, pathes: Map<number, string>): void {
this._idToAppClip = clips;
this._idToAppPath = pathes;
}
/**
* 設定AudioID
* @param id
* @param audioId
*/
private _setAppAudioId(id: number, audioId: number): void {
this._idToAppAudioId.set(id, audioId);
}
/**
* 打開音效音量
*/
public OpenSound(): void {
CSAudio.Instance.OpenSound();
}
/**
* 關閉音效音量
*/
public CloseSound(): void {
CSAudio.Instance.CloseSound();
}
/**
* 取得AudioID
* @param id
*/
private _getAppAudioId(id: number): number {
if (this._idToAppAudioId.has(id)) {
return this._idToAppAudioId.get(id);
} else {
return -1;
}
}
/**
* 播放音效
* @param id
* @param loop
*/
public PlayAppSound(id: number, loop: boolean = false): void {
// 靜音後有一禎仍然會撥放的問題:音量>0才撥放
if (cc.audioEngine.getEffectsVolume() <= 0) {
return;
}
if (this._idToAppClip.has(id)) {
let path: string = this._idToAppPath.get(id);
let timenum: number = new Date().getTime();
if (!this._lastAppPlaySoundTime.has(path)) {
this._lastAppPlaySoundTime.set(path, timenum);
let audioId: number = cc.audioEngine.playEffect(this._idToAppClip.get(id), loop);
this._setAppAudioId(id, audioId);
} else {
let lastTime: number = this._lastAppPlaySoundTime.get(path);
if (timenum - lastTime > this._appCanPlaySoundCutTime) {
this._lastAppPlaySoundTime.set(path, timenum);
let audioId: number = cc.audioEngine.playEffect(this._idToAppClip.get(id), loop);
this._setAppAudioId(id, audioId);
}
}
} else {
cc.error("未知的Sound Id: ", id);
}
}
/**
* 停止音效
* @param id
*/
public StopAppSound(id: number): void {
let audioId: number = this._getAppAudioId(id);
if (audioId >= 0) {
cc.audioEngine.stopEffect(audioId);
}
}
/**
* 打開音樂音量
*/
public OpenMusic(): void {
this._setAppMusicVolume(0.3);
CSAudio.Instance.OpenMusic();
}
/**
* 關閉音樂音量
*/
public CloseMusic(): void {
this._setAppMusicVolume(0.0); // 這邊再改改
CSAudio.Instance.CloseMusic();
}
/**
* 設定音樂音量
* @param volume
*/
private _setAppMusicVolume(volume: number): void {
// 靜音後有一禎仍然會撥放的問題:背景音樂要回復
if (this._currentAppMusic != -1 && volume > 0 && !cc.audioEngine.isMusicPlaying()) {
this._ccAppMusicPlayId = cc.audioEngine.playMusic(this._idToAppClip.get(this._currentAppMusic), true);
}
}
/**
* 撥放音樂
* @param id
* @param loop
*/
public PlayAppMusic(id: number, loop: boolean = true): void {
if (this._currentAppMusic != id) {
if (this._idToAppClip.has(id)) {
// 靜音後有一禎仍然會撥放的問題:音量>0才撥放
if (cc.audioEngine.getMusicVolume() > 0) {
this._ccAppMusicPlayId = cc.audioEngine.playMusic(this._idToAppClip.get(id), loop);
}
this._currentAppMusic = id;
} else {
cc.error("未知的Music Id: ", id);
}
} else {
this.StopMusic();
this.PlayAppMusic(id);
}
}
private _ccAppMusicPlayId: number = -1;
private _currentAppMusicTime: number = -1;
public pauseOrResume(isPause?: boolean): void {
if (isPause) {
this._currentAppMusicTime = cc.audioEngine.getCurrentTime(this._ccAppMusicPlayId);
cc.audioEngine.pauseAll();
cc.audioEngine.stopMusic();
} else {
cc.audioEngine.resumeAll();
cc.audioEngine.setCurrentTime(this._ccAppMusicPlayId, this._currentAppMusicTime);
}
}
/**
* 停止音樂
* @param id
*/
public StopMusic(): void {
cc.audioEngine.stopMusic();
this._currentAppMusic = -1;
}
}