From 01f05f4824baba973e97b186efc428e337051127 Mon Sep 17 00:00:00 2001 From: kron Date: Wed, 27 Aug 2025 18:23:59 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A3=B0=E9=9F=B3=E6=92=AD=E6=94=BE=E8=A7=84?= =?UTF-8?q?=E5=88=99=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/audioManager.js | 34 +++++++++++++++++++++++++++++-- src/components/HeaderProgress.vue | 12 ++++++----- src/components/ShootProgress.vue | 12 ++++++----- 3 files changed, 46 insertions(+), 12 deletions(-) diff --git a/src/audioManager.js b/src/audioManager.js index 3066a3b..96a53cc 100644 --- a/src/audioManager.js +++ b/src/audioManager.js @@ -52,6 +52,8 @@ const audioFils = { class AudioManager { constructor() { this.audioMap = new Map(); + this.currentPlayingKey = null; // 记录当前播放的音频key + Object.keys(audioFils).forEach((key) => { const audio = uni.createInnerAudioContext(); audio.src = audioFils[key]; @@ -66,20 +68,47 @@ class AudioManager { console.log(`音频 ${key} 加载失败:`, res.errMsg); }); + // 监听播放结束事件 + audio.onEnded(() => { + if (this.currentPlayingKey === key) { + this.currentPlayingKey = null; + } + }); + + // 监听播放停止事件 + audio.onStop(() => { + if (this.currentPlayingKey === key) { + this.currentPlayingKey = null; + } + }); + this.audioMap.set(key, audio); }); } // 播放指定音频 play(key) { + // 如果有正在播放的音频,先停止 + if (this.currentPlayingKey) { + this.stop(this.currentPlayingKey); + } + const audio = this.audioMap.get(key); - if (audio) audio.play(); + if (audio) { + audio.play(); + this.currentPlayingKey = key; + } } // 停止指定音频 stop(key) { const audio = this.audioMap.get(key); - if (audio) audio.stop(); + if (audio) { + audio.stop(); + if (this.currentPlayingKey === key) { + this.currentPlayingKey = null; + } + } } // 销毁所有音频实例 @@ -88,6 +117,7 @@ class AudioManager { audio.destroy(); }); this.audioMap.clear(); + this.currentPlayingKey = null; } } diff --git a/src/components/HeaderProgress.vue b/src/components/HeaderProgress.vue index 6b0e2ee..760d857 100644 --- a/src/components/HeaderProgress.vue +++ b/src/components/HeaderProgress.vue @@ -59,11 +59,13 @@ async function onReceiveMessage(messages = []) { audioManager.play(currentSound.value); } } else if (msg.constructor === MESSAGETYPES.InvalidShot) { - uni.showToast({ - title: "距离不足,无效", - icon: "none", - }); - audioManager.play("射击无效"); + if (msg.userId === user.value.id) { + uni.showToast({ + title: "距离不足,无效", + icon: "none", + }); + audioManager.play("射击无效"); + } } else if (msg.constructor === MESSAGETYPES.AllReady) { currentRoundEnded.value = true; audioManager.play("比赛开始"); diff --git a/src/components/ShootProgress.vue b/src/components/ShootProgress.vue index 8f52dcf..ce3ed69 100644 --- a/src/components/ShootProgress.vue +++ b/src/components/ShootProgress.vue @@ -136,11 +136,13 @@ async function onReceiveMessage(messages = []) { audioManager.play(currentSound.value); } } else if (msg.constructor === MESSAGETYPES.InvalidShot) { - uni.showToast({ - title: "距离不足,无效", - icon: "none", - }); - audioManager.play("射击无效"); + if (msg.userId === user.value.id) { + uni.showToast({ + title: "距离不足,无效", + icon: "none", + }); + audioManager.play("射击无效"); + } } else if (msg.constructor === MESSAGETYPES.AllReady) { audioManager.play("比赛开始"); } else if (msg.constructor === MESSAGETYPES.MeleeAllReady) {