feat: 星域故事汇小游戏初始版本
This commit is contained in:
132
client/js/data/AudioManager.js
Normal file
132
client/js/data/AudioManager.js
Normal file
@@ -0,0 +1,132 @@
|
||||
/**
|
||||
* 音频管理器
|
||||
*/
|
||||
export default class AudioManager {
|
||||
constructor() {
|
||||
this.bgm = null;
|
||||
this.sfx = {};
|
||||
this.isMuted = false;
|
||||
this.bgmVolume = 0.5;
|
||||
this.sfxVolume = 0.8;
|
||||
}
|
||||
|
||||
/**
|
||||
* 播放背景音乐
|
||||
*/
|
||||
playBGM(src) {
|
||||
if (this.isMuted || !src) return;
|
||||
|
||||
// 停止当前BGM
|
||||
this.stopBGM();
|
||||
|
||||
// 创建新的音频实例
|
||||
this.bgm = wx.createInnerAudioContext();
|
||||
this.bgm.src = src;
|
||||
this.bgm.loop = true;
|
||||
this.bgm.volume = this.bgmVolume;
|
||||
this.bgm.play();
|
||||
}
|
||||
|
||||
/**
|
||||
* 停止背景音乐
|
||||
*/
|
||||
stopBGM() {
|
||||
if (this.bgm) {
|
||||
this.bgm.stop();
|
||||
this.bgm.destroy();
|
||||
this.bgm = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 暂停背景音乐
|
||||
*/
|
||||
pauseBGM() {
|
||||
if (this.bgm) {
|
||||
this.bgm.pause();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 恢复背景音乐
|
||||
*/
|
||||
resumeBGM() {
|
||||
if (this.bgm && !this.isMuted) {
|
||||
this.bgm.play();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 播放音效
|
||||
*/
|
||||
playSFX(name, src) {
|
||||
if (this.isMuted || !src) return;
|
||||
|
||||
// 复用或创建音效实例
|
||||
if (!this.sfx[name]) {
|
||||
this.sfx[name] = wx.createInnerAudioContext();
|
||||
this.sfx[name].src = src;
|
||||
}
|
||||
|
||||
this.sfx[name].volume = this.sfxVolume;
|
||||
this.sfx[name].seek(0);
|
||||
this.sfx[name].play();
|
||||
}
|
||||
|
||||
/**
|
||||
* 播放点击音效
|
||||
*/
|
||||
playClick() {
|
||||
// 可以配置点击音效
|
||||
// this.playSFX('click', 'audio/click.mp3');
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置静音
|
||||
*/
|
||||
setMute(muted) {
|
||||
this.isMuted = muted;
|
||||
if (muted) {
|
||||
this.pauseBGM();
|
||||
} else {
|
||||
this.resumeBGM();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 切换静音状态
|
||||
*/
|
||||
toggleMute() {
|
||||
this.setMute(!this.isMuted);
|
||||
return this.isMuted;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置BGM音量
|
||||
*/
|
||||
setBGMVolume(volume) {
|
||||
this.bgmVolume = Math.max(0, Math.min(1, volume));
|
||||
if (this.bgm) {
|
||||
this.bgm.volume = this.bgmVolume;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置音效音量
|
||||
*/
|
||||
setSFXVolume(volume) {
|
||||
this.sfxVolume = Math.max(0, Math.min(1, volume));
|
||||
}
|
||||
|
||||
/**
|
||||
* 销毁所有音频
|
||||
*/
|
||||
destroy() {
|
||||
this.stopBGM();
|
||||
Object.values(this.sfx).forEach(audio => {
|
||||
audio.stop();
|
||||
audio.destroy();
|
||||
});
|
||||
this.sfx = {};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user