声音播放规则修改

This commit is contained in:
kron
2025-08-27 18:23:59 +08:00
parent eb076df7d5
commit 01f05f4824
3 changed files with 46 additions and 12 deletions

View File

@@ -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;
}
}

View File

@@ -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("比赛开始");

View File

@@ -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) {