Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
83
rated 0 times [  89] [ 6]  / answers: 1 / hits: 7586  / 3 Years ago, tue, may 25, 2021, 12:00:00

I am trying to trigger an alarm after a certain time has ended. I am using Howler.js to play the alarm, but for some reason, it is displaying this error: howler.js:2500 The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page. I switched to Howler because a similar error was coming when I tried to use Audio() Web API in JavaScript.


Here is my code:


const { Howl, Howler } = require("howler");

let i;
let timer;
var alarm = new Howl({
src: ["alarm.mp3"],
});
let playTimer = true;
const timerDisplay = document.getElementById("timerDisplay");

document.addEventListener("DOMContentLoaded", () => {
i = 5;

timer = setInterval(() => {
if (playTimer == true) {
if (i != 0) {
timerDisplay.textContent = i;
i--;
}

if (i == 0) {
alarm.play();
clearInterval(timer);
}
}
}, 1000);
});


I don't know if this is helpful or not, but just to let you know, I am using Parcel as my bundler. Thank you in advance for any answer.


More From » node.js

 Answers
3

As @Kokodoko said in the comments of my question, the audio had to be triggered by some form of user interaction. So, I added a button in my HTML and replaced the


document.addEventListener("DOMContentLoaded", () => {})


with


startTimerBtn.addEventListener("click", () => {})


So this is the final code:


const { Howl, Howler } = require("howler");

let i;
let timer;
var alarm = new Howl({
src: ["alarm.mp3"],
});
let playTimer = true;
const timerDisplay = document.getElementById("timerDisplay");
const startTimerBtn = document.getElementById("startTimer");

startTimerBtn.addEventListener("click", () => {
i = 5;

timer = setInterval(() => {
if (playTimer == true) {
if (i != 0) {
timerDisplay.textContent = i;
i--;
}

if (i == 0) {
alarm.play();
clearInterval(timer);
}
}
}, 1000);
});


Although this wasn't the solution I intended to get, it suits my needs well.


Also, one small other thing for others using Parcel.js like me. Just in case you don't know, you have to put your audio file in Parcel's dist directory so that it can play. For some reason, it wasn't bundling automatically.


[#1317] Tuesday, May 18, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
luna

Total Points: 698
Total Questions: 114
Total Answers: 93

Location: Israel
Member since Wed, Apr 14, 2021
3 Years ago
luna questions
Sun, Jul 11, 21, 00:00, 3 Years ago
Wed, Jun 30, 21, 00:00, 3 Years ago
Sun, Apr 25, 21, 00:00, 3 Years ago
Tue, Oct 13, 20, 00:00, 4 Years ago
;