0
Why audio is not working? (Javascript)
Here is the code: https://code.sololearn.com/WJlKrk6aZSYK/?ref=app Please, tell me how to fix it.
2 Respostas
+ 1
The JavaScript console says this after I run it in Chrome and input 3 to the prompt:
"Uncaught (in promise) DOMException: play() failed because the user didn't interact with the document first. https://goo.gl/xX8pDD"
Getting the user to type the number of seconds into the web page instead of through your prompt should fix it.
Here are the changes to make that work:
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<title>ALARM</title>
</head>
<body>
<p>How long after you want to play the alarm.(only seconds)</p>
<input type="number" value="3">
<button>Play Sound</button>
</body>
</html>
JavaScript:
document.addEventListener('DOMContentLoaded', function() {
var c = new Audio("https://dl.dropboxusercontent.com/s/fadmg8shhwht3vq/alarm.mp3?dl=0");
document.querySelector('button').addEventListener('click', function() {
function playSound() {
c.play();
}
var a = parseInt(document.querySelector('input').value);
var b = a*1000;
setTimeout(playSound, b);
});
});
+ 1
Thanks a lot.