Browse Source

add isplaying prevent binding audio

pull/14/head^2
PhamY0601 1 year ago
parent
commit
f9143b66ed
2 changed files with 23 additions and 23 deletions
  1. +3
    -6
      src/app/modules/homepage/centralized-security-management/centralized-security-management.component.ts
  2. +20
    -17
      src/app/shared/services/alarm-sound.service.ts

+ 3
- 6
src/app/modules/homepage/centralized-security-management/centralized-security-management.component.ts View File

@@ -102,14 +102,11 @@ export class CentralizedSecurityManagementComponent
this.state2 = message.state2 === '0'; // 1 OFF // alarm 1h
this.state5 = message.state5 === '1'; // 1 ON, 0 OFF
this.isReady = message.ready === '1';
this.alarmSoundService$.startAlarm(
this.state5,
this.isReady,
this.state1,
this.state2,
);
this.alarmSoundService$.startAlarm(this.state5, this.isReady, this.state1, this.state2);
this.updateIcons();

}

}

addIconsToMap(): void {

+ 20
- 17
src/app/shared/services/alarm-sound.service.ts View File

@@ -1,4 +1,4 @@
import { ElementRef, Injectable } from '@angular/core';
import { Injectable } from '@angular/core';

@Injectable({
providedIn: 'root',
@@ -7,35 +7,41 @@ export class AlarmSoundService {
private alertInterval: any;
private alertDuration: number = 10000;
private audio = new Audio();
private isPlaying: boolean = false;

constructor() {
this.audio.src = 'assets/sound/alarm_5m.mp3';
this.audio.load();
this.audio.onended = () => {
// Khi phát hết âm thanh, đặt biến isPlaying về false
this.isPlaying = false;
};
}

playSound(): void {
this.audio.play().catch((error) => {
console.error('Error playing audio:', error);
});
if (!this.isPlaying) {
this.audio.play().then(() => {
this.isPlaying = true;
this.alertInterval = setInterval(() => {
this.stopAlarm();
}, this.alertDuration);
}).catch((error) => {
console.error('Error playing audio:', error);
});
}
}

stopSound(): void {
if (this.audio) {
this.audio.pause();
this.audio.currentTime = 0;
this.isPlaying = false;
}
}

startAlarm(
state5: boolean,
isReady: boolean,
state1: boolean,
state2: boolean,
): void {
if (state5 && isReady && (state1 || state2)) {
startAlarm( isTurnOn: boolean, isReady: boolean, fireArm: boolean, fenceArm: boolean): void {
if (isTurnOn && isReady && (fireArm || fenceArm)) {
this.playSound();
this.alertInterval = setInterval(() => {
this.stopAlarm();
}, this.alertDuration);
}
}

@@ -44,7 +50,4 @@ export class AlarmSoundService {
clearInterval(this.alertInterval);
}

simulateClick(element: HTMLElement) {
element.click();
}
}

Loading…
Cancel
Save