'JavaScript audio.play() error
my code stopped working today (it worked before).
Here is my code:
playMe = new Audio(link);
playMe.play();
Here is the error I get in the console:
Uncaught (in promise) DOMException: Failed to load because no supported source was found.
It is really the audio.play(); that doesn't work because if I only keep the first line I don't get an error (but obviously is dos't play).
I'm using chrome 52.0.2743.82 m (64-bit)
Thanks.
Solution 1:[1]
Before you play audio, window must be focused. For this case you can use:
playMe = new Audio(link);
window.focus();
playMe.play();
Solution 2:[2]
The below answer is for video. The same you can use for audio as well.
var playPromise = document.querySelector('video').play();
// In browsers that don’t yet support this functionality,
// playPromise won’t be defined.
if (playPromise !== undefined) {
playPromise.then(function() {
// Automatic playback started!
}).catch(function(error) {
// Automatic playback failed.
// Show a UI element to let the user manually start playback.
});
}
Solution 3:[3]
I had the exact problem as you.
I tried playing a <audio> html element and it worked!
So I thought that maybe...
The only thing you should do is to append playMe to body...
var play_me = new Audio("music.mp3");
document.body.appendChild(play_me);
play_me.play();
But don't worry the play_me isn't visible.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Nhat Ngo |
| Solution 2 | jay rangras |
| Solution 3 | Front Cutted |
