Playing local mp3 file in Visual Studio Code?

I want to play an mp3 file in visual studio Code using Javascript , i have the file in my folder but it won’t play.

I tested it with a weblink to an mp3 file and that works .

How to make it work with my local file?

Have you checked the console for errors? Is the path correct (are you in the same folder as the sound file?)

Pretty sure the browser is going to block the call to play because there is no user interaction.

Chrome:

Uncaught (in promise) DOMException: play() failed because the user didn't interact with the document first.

Firefox:

Uncaught (in promise) DOMException: The play method is not allowed by the user agent or the platform in the current context, possibly because the user denied permission.


Edit:

Using an audio element in the DOM still works in Chrome but is blocked in Firefox.

Example which works in Chrome, but not Firefox:
https://codepen.io/lasjorg/pen/MWgJRaq

With user interaction (works in both)
https://codepen.io/lasjorg/pen/GRKreyJ

If you had an element inside the DOM, like a play button, it should work as well with the Audio constructor in both browsers.

const audio = new Audio('someSong.mp3');
const btn = document.querySelector('#play');

btn.addEventListener('click', () => {
  audio.play();
});

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.