TS
limitExceed(params: any) {
params.forEach((data: any) => {
if (data.percent === 100) {
this.createNotification('warning', data.sensor, false);
} else if (data.percent >= 56 && data.percent <= 99.99) {
this.createNotification('warning', data.sensor, true);
}
});
}
createNotification(type: string, name: string, types: boolean): void {
const textColor = '#ff0000';
const title = 'High humidity!';
let subtitle: any;
timer(1000, 300000).subscribe(() => {
// const subTitle: any;
if (types) {
subtitle = name + 'humidity has reached the minimum limit';
} else {
subtitle = name + ' humidity has reached the maximum';
}
this.notification.config({
nzPlacement: 'bottomRight',
nzDuration: 5000,
});
this.playWithAudio(type, title, subtitle, textColor);
});
}
playWithAudio(type: string, title: string, subtitle: string, textColor: string) {
const AUDIO = <HTMLMediaElement>document.getElementById('audio');
if (AUDIO) {
AUDIO.muted = true; // temporarily revolves play error on browser
const playPromise = AUDIO.play();
this.notification.create(type, title, subtitle, {
nzStyle: { color: textColor, 'border-left': textColor + ' 5px solid' }
});
if (playPromise !== null) {
playPromise.then(() => { AUDIO.play(); })
.catch(error => { AUDIO.play(); });
}
}
}
HTML
<audio id="audio" hidden>
<source src="./assets/audio/notif.mp3" type="audio/wav" />
</audio>
Как исправить ошибку ERROR: Uncaught (в обещании): NotAllowedError: play () при перезагрузке страницы?
причина, когда я запускаю приложение, звук работает, но когда я пытаюсь перезагрузить предупреждение / уведомление работает, но звук не работает, потому что есть ошибка. Который является ошибкой: Uncaught (в обещании): NotAllowedError: play ()
Как я это исправлю?