mirror of
https://gitee.com/dgflash/oops-plugin-framework.git
synced 2026-06-30 07:24:26 +08:00
201 lines
5.3 KiB
TypeScript
201 lines
5.3 KiB
TypeScript
import { Component } from "cc";
|
|
import { oops } from "../../Oops";
|
|
import { AudioEffect } from "./AudioEffect";
|
|
import { AudioMusic } from "./AudioMusic";
|
|
|
|
const LOCAL_STORE_KEY = "game_audio";
|
|
|
|
/**
|
|
* 音频管理
|
|
* @example
|
|
// 模块功能通过 oops.audio 调用
|
|
oops.audio.playMusic("audios/nocturne");
|
|
*/
|
|
export class AudioManager extends Component {
|
|
private local_data: any = {};
|
|
|
|
private music!: AudioMusic;
|
|
private effect!: AudioEffect;
|
|
|
|
private _volume_music: number = 1;
|
|
private _volume_effect: number = 1;
|
|
private _switch_music: boolean = true;
|
|
private _switch_effect: boolean = true;
|
|
|
|
/**
|
|
* 设置背景音乐播放完成回调
|
|
* @param callback 背景音乐播放完成回调
|
|
*/
|
|
setMusicComplete(callback: Function | null = null) {
|
|
this.music.onComplete = callback;
|
|
}
|
|
|
|
/**
|
|
* 播放背景音乐
|
|
* @param url 资源地址
|
|
* @param callback 音乐播放完成事件
|
|
*/
|
|
playMusic(url: string, callback?: Function) {
|
|
if (this._switch_music) {
|
|
this.music.load(url, callback);
|
|
}
|
|
}
|
|
|
|
/** 循环播放背景音乐 */
|
|
playerMusicLoop(url: string) {
|
|
if (this._switch_music) {
|
|
this.music.loop = true;
|
|
this.music.load(url);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取背景音乐播放进度
|
|
*/
|
|
get progressMusic(): number {
|
|
return this.music.progress;
|
|
}
|
|
/**
|
|
* 设置背景乐播放进度
|
|
* @param value 播放进度值
|
|
*/
|
|
set progressMusic(value: number) {
|
|
this.music.progress = value;
|
|
}
|
|
|
|
/**
|
|
* 获取背景音乐音量
|
|
*/
|
|
get volumeMusic(): number {
|
|
return this._volume_music;
|
|
}
|
|
/**
|
|
* 设置背景音乐音量
|
|
* @param value 音乐音量值
|
|
*/
|
|
set volumeMusic(value: number) {
|
|
this._volume_music = value;
|
|
this.music.volume = value;
|
|
}
|
|
|
|
/**
|
|
* 获取背景音乐开关值
|
|
*/
|
|
get switchMusic(): boolean {
|
|
return this._switch_music;
|
|
}
|
|
/**
|
|
* 设置背景音乐开关值
|
|
* @param value 开关值
|
|
*/
|
|
set switchMusic(value: boolean) {
|
|
this._switch_music = value;
|
|
|
|
if (value == false)
|
|
this.music.stop();
|
|
}
|
|
|
|
/**
|
|
* 播放音效
|
|
* @param url 资源地址
|
|
*/
|
|
playEffect(url: string) {
|
|
if (this._switch_effect) {
|
|
this.effect.load(url);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取音效音量
|
|
*/
|
|
get volumeEffect(): number {
|
|
return this._volume_effect;
|
|
}
|
|
/**
|
|
* 设置获取音效音量
|
|
* @param value 音效音量值
|
|
*/
|
|
set volumeEffect(value: number) {
|
|
this._volume_effect = value;
|
|
this.effect.volume = value;
|
|
}
|
|
|
|
/**
|
|
* 获取音效开关值
|
|
*/
|
|
get switchEffect(): boolean {
|
|
return this._switch_effect;
|
|
}
|
|
/**
|
|
* 设置音效开关值
|
|
* @param value 音效开关值
|
|
*/
|
|
set switchEffect(value: boolean) {
|
|
this._switch_effect = value;
|
|
if (value == false)
|
|
this.effect.stop();
|
|
}
|
|
|
|
/** 恢复当前暂停的音乐与音效播放 */
|
|
resumeAll() {
|
|
if (this.music) {
|
|
this.music.play();
|
|
this.effect.play();
|
|
}
|
|
}
|
|
|
|
/** 暂停当前音乐与音效的播放 */
|
|
pauseAll() {
|
|
if (this.music) {
|
|
this.music.pause();
|
|
this.effect.pause();
|
|
}
|
|
}
|
|
|
|
/** 停止当前音乐与音效的播放 */
|
|
stopAll() {
|
|
if (this.music) {
|
|
this.music.stop();
|
|
this.effect.stop();
|
|
}
|
|
}
|
|
|
|
/** 保存音乐音效的音量、开关配置数据到本地 */
|
|
save() {
|
|
this.local_data.volume_music = this._volume_music;
|
|
this.local_data.volume_effect = this._volume_effect;
|
|
this.local_data.switch_music = this._switch_music;
|
|
this.local_data.switch_effect = this._switch_effect;
|
|
|
|
let data = JSON.stringify(this.local_data);
|
|
oops.storage.set(LOCAL_STORE_KEY, data);
|
|
}
|
|
|
|
|
|
/** 本地加载音乐音效的音量、开关配置数据并设置到游戏中 */
|
|
load() {
|
|
this.music = this.getComponent(AudioMusic) || this.addComponent(AudioMusic)!;
|
|
this.effect = this.getComponent(AudioEffect) || this.addComponent(AudioEffect)!;
|
|
|
|
let data = oops.storage.get(LOCAL_STORE_KEY);
|
|
if (data) {
|
|
try {
|
|
this.local_data = JSON.parse(data);
|
|
this._volume_music = this.local_data.volume_music;
|
|
this._volume_effect = this.local_data.volume_effect;
|
|
this._switch_music = this.local_data.switch_music;
|
|
this._switch_effect = this.local_data.switch_effect;
|
|
}
|
|
catch (e) {
|
|
this.local_data = {};
|
|
this._volume_music = 1;
|
|
this._volume_effect = 1;
|
|
this._switch_music = true;
|
|
this._switch_effect = true;
|
|
}
|
|
|
|
if (this.music) this.music.volume = this._volume_music;
|
|
if (this.effect) this.effect.volume = this._volume_effect;
|
|
}
|
|
}
|
|
} |