WetlandGuard-Admin/src/utils/SoundEffect.js

35 lines
922 B
JavaScript

class SoundEffect {
constructor() {
this.SOUND_CONNECT = new Audio('/src/assets/harmonyos-sound/notification_accomplished_08.wav');
this.SOUND_DISCONNECT = new Audio('/src/assets/harmonyos-sound/notification_wrong_04.wav');
}
// 播放连接成功音效
playConnectSound() {
this.SOUND_CONNECT.currentTime = 0;
this.SOUND_CONNECT.play().catch(error => {
console.error('音效播放失败:', error);
});
}
// 播放断开连接音效
playDisconnectSound() {
this.SOUND_DISCONNECT.currentTime = 0;
this.SOUND_DISCONNECT.play().catch(error => {
console.error('音效播放失败:', error);
});
}
// 根据状态播放对应音效
playStatusSound(isOnline) {
if (isOnline) {
this.playConnectSound();
} else {
this.playDisconnectSound();
}
}
}
// 创建单例实例
const soundEffect = new SoundEffect();
export default soundEffect;