声音播放规则修改
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user