skip to Main Content

Alright, so I’m trying to use audio files from the internet with JavaScript.
My code is:

let audio = new Audio('https://prosearch.tribeofnoise.com/artists/show/78865/38605.mp3');
audio.play();

However it doesn’t work, and I don’t know how to fix the format. Please help.

I tried the code above ~^, and I expected the royalty free song to play. However nothing happened whatsoever! I’m just trying to find out what’s wrong with it

2

Answers


  1. Can you confirm that the audio is being played but the volume may be too low? Or that the audio is actually accessible? I tried to create something similar and experienced similar issues until I implemented a button that triggers the audio. If you are using chrome it may disable auto play.

    I used a button and it worked.

    <button id="playButton">Play Audio</button>
    <script>
      let audio = new Audio('https://prosearch.tribeofnoise.com/artists/show/78865/38605.mp3');
      document.getElementById('playButton').addEventListener('click', () => {
        audio.play();
      });
    </script>
    
    Login or Signup to reply.
  2. Modern browsers restrict automatic media playback, which is a standard across all browsers with limited exceptions. Consequently, media can only be played with user interaction on the page. A potential solution is to present a full-screen prompt when the user enters the page, giving them the choice to dismiss it, confirm playback, or require confirmation for proceeding.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search