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

this.state2 = message.state2 === '0'; // 1 OFF // alarm 1h this.state2 = message.state2 === '0'; // 1 OFF // alarm 1h
this.state5 = message.state5 === '1'; // 1 ON, 0 OFF this.state5 = message.state5 === '1'; // 1 ON, 0 OFF
this.isReady = message.ready === '1'; 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(); this.updateIcons();

} }

} }


addIconsToMap(): void { addIconsToMap(): void {

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

import { ElementRef, Injectable } from '@angular/core';
import { Injectable } from '@angular/core';


@Injectable({ @Injectable({
providedIn: 'root', providedIn: 'root',
private alertInterval: any; private alertInterval: any;
private alertDuration: number = 10000; private alertDuration: number = 10000;
private audio = new Audio(); private audio = new Audio();
private isPlaying: boolean = false;

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


playSound(): void { 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 { stopSound(): void {
if (this.audio) { if (this.audio) {
this.audio.pause(); this.audio.pause();
this.audio.currentTime = 0; 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.playSound();
this.alertInterval = setInterval(() => {
this.stopAlarm();
}, this.alertDuration);
} }
} }


clearInterval(this.alertInterval); clearInterval(this.alertInterval);
} }


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

Loading…
Cancel
Save