Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 670
Zvi Karp Avatar asked Jan 18 '26 04:01

Zvi Karp


2 Answers

Before you play audio, window must be focused. For this case you can use:

playMe = new Audio(link);
window.focus();
playMe.play();
like image 81
Nhat Ngo Avatar answered Jan 20 '26 16:01

Nhat Ngo


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.
});
}
like image 23
jay rangras Avatar answered Jan 20 '26 17:01

jay rangras



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!