Files
2026-02-10 09:56:08 +08:00

56 lines
1.1 KiB
JavaScript

// 蜂鸣音效管理器
class Beeper {
audioContext = null;
// 初始化音频上下文
initAudioContext() {
if (!this.audioContext) {
this.audioContext = uni.createInnerAudioContext();
this.audioContext.autoplay = false;
this.audioContext.loop = true;
this.audioContext.src = '/static/beep_01.mp3';
}
return this.audioContext;
}
// 播放蜂鸣声
play() {
if (this.audioContext) {
try {
this.audioContext.pause();
this.audioContext.play();
} catch (e) {
// 忽略播放失败的错误
console.log('播放失败:', e);
}
} else{
// 创建AudioContext实例
let audioContext = this.initAudioContext();
audioContext.play();
}
}
// 停止蜂鸣声
stop() {
if (this.audioContext) {
console.log('停止播放');
this.audioContext.pause();
}
}
}
// 创建全局实例
const beeper = new Beeper();
// 导出函数
export default {
play() {
return beeper.play();
},
// 停止播放
stop() {
return beeper.stop();
},
};