From ddef52e6ceb13468e936eba8e053fa36365e7a4c Mon Sep 17 00:00:00 2001 From: ZF sun <34314687@qq.com> Date: Tue, 16 Dec 2025 08:29:14 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E7=BA=BF=E4=B8=8A=E8=83=BD=E5=A4=9F?= =?UTF-8?q?=E6=92=AD=E6=94=BE=E9=9F=B3=E4=B9=90=E7=9A=84=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/musicPlayer.js | 72 ++++++++++++++++++------------------- src/views/BattleRanking.vue | 11 +++++- 2 files changed, 46 insertions(+), 37 deletions(-) diff --git a/src/utils/musicPlayer.js b/src/utils/musicPlayer.js index 4b02f01..0c7750c 100644 --- a/src/utils/musicPlayer.js +++ b/src/utils/musicPlayer.js @@ -1,10 +1,10 @@ // src/utils/musicPlayer.js class MusicPlayer { constructor() { - this.audio = null; - this.isPlaying = false; - this.defaultPath = ""; - this.enabled = false; + this.audio = null; + this.isPlaying = false; + this.defaultPath = ""; + this.enabled = false; this.volume = 0.5; // 默认音量50% } @@ -24,30 +24,30 @@ class MusicPlayer { */ initMusicConfig(filePath, enabled, volume = 0.5) { console.log("初始化音乐配置:", { filePath, enabled, volume }); - + this.enabled = enabled; // 确保音量是数字类型 this.volume = typeof volume === 'string' ? parseFloat(volume) : volume; console.log("处理后的音量值:", this.volume, "类型:", typeof this.volume); - + let validPath = this.defaultPath; if (filePath && filePath.endsWith('.mp3')) { validPath = filePath; } else if (filePath) { console.warn(`音乐路径无效(非MP3格式):${filePath},使用兜底路径`); } - + console.log("使用的音乐路径:", validPath); - + if (this.audio) { this.audio.pause(); this.audio = null; } - + this.audio = new Audio(validPath); this.audio.loop = true; this.audio.volume = this.volume; - + console.log("音频对象创建完成,音量设置为:", this.audio.volume); } @@ -56,22 +56,22 @@ class MusicPlayer { */ play() { console.log("调用 play 方法,当前状态:", { enabled: this.enabled, hasAudio: !!this.audio, isPlaying: this.isPlaying }); - + if (!this.enabled) { console.log("首页播放开关未开启,跳过音乐播放"); return; } - + if (!this.audio) { console.warn("音频对象未初始化"); this.initMusicConfig(this.defaultPath, false); console.warn("未初始化音乐配置,使用兜底路径且关闭播放开关"); return; } - + console.log("音频源路径:", this.audio.src); console.log("音频就绪状态:", this.audio.readyState); - + if (!this.isPlaying) { this.audio.play() .then(() => { @@ -102,31 +102,31 @@ class MusicPlayer { console.log("音乐已暂停"); } } -/** - * 新增:设置静音/取消静音(适配管理员/首页场景) - * @param {boolean} muted 是否静音 - */ -setMuted(muted) { - if (this.audio) { - this.audio.muted = muted; - console.log(muted ? "音乐已静音" : "音乐已取消静音"); + /** + * 新增:设置静音/取消静音(适配管理员/首页场景) + * @param {boolean} muted 是否静音 + */ + setMuted(muted) { + if (this.audio) { + this.audio.muted = muted; + console.log(muted ? "音乐已静音" : "音乐已取消静音"); + } } -} -/** - * 设置音量 - * @param {number} volume 音量值 (0.0 到 1.0) - */ -setVolume(volume) { - if (this.audio) { - // 确保音量是数字类型 - const numericVolume = typeof volume === 'string' ? parseFloat(volume) : volume; - // 限制音量范围在0.0到1.0之间 - this.volume = Math.max(0, Math.min(1, numericVolume)); - this.audio.volume = this.volume; - console.log(`音乐音量已设置为: ${Math.round(this.volume * 100)}%`); + /** + * 设置音量 + * @param {number} volume 音量值 (0.0 到 1.0) + */ + setVolume(volume) { + if (this.audio) { + // 确保音量是数字类型 + const numericVolume = typeof volume === 'string' ? parseFloat(volume) : volume; + // 限制音量范围在0.0到1.0之间 + this.volume = Math.max(0, Math.min(1, numericVolume)); + this.audio.volume = this.volume; + console.log(`音乐音量已设置为: ${Math.round(this.volume * 100)}%`); + } } -} /** * 新增:stop 方法(组件 onUnmounted 调用,暂停+重置进度) diff --git a/src/views/BattleRanking.vue b/src/views/BattleRanking.vue index c077878..55e1905 100644 --- a/src/views/BattleRanking.vue +++ b/src/views/BattleRanking.vue @@ -1,5 +1,5 @@