3

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.

Zvi Karp
  • 3,621
  • 3
  • 25
  • 40

3 Answers3

1

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

playMe = new Audio(link);
window.focus();
playMe.play();
Nhat Ngo
  • 113
  • 2
  • 9
1

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.
});
}
jay rangras
  • 135
  • 1
  • 1
  • 11
0

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.

Front Cutted
  • 9
  • 1
  • 3