skip to Main Content

So I made a website that was supposed to redirect the person to another website where they were going to be rickrolled. The GIF of Rick worked fine, but for some reason, I just can’t get the audio file to play. Redirecting to the new website works fine and there are no problems with that.

let audioo = document.getElementById("audi");
audi.play();
console.log("audio funksjonen kjører");
<audio id="audi" src="./RickRoll.mp3" autoplay></audio>

I tried to use the autoplay attribute in the audio tag, but that did not work. The mp3 file is in the same folder as the HTML and CSS files, so I don’t think the program doesn’t see the mp3 file.

2

Answers


  1. <!DOCTYPE html>
    <html>
    
    <head>
      <title>Rickroll Page</title>
    </head>
    
    <body>
      <!-- Audio element -->
      <audio id="audi" src="https://www.computerhope.com/jargon/m/example.mp3"></audio>
    
      <!-- Button to trigger audio play -->
      <button onclick="playAudio()">Play Audio</button>
    
      <script>
        function playAudio() {
          var audio = document.getElementById("audi");
          audio.play();
        }
      </script>
    </body>
    
    </html>

    In your code, the autoplay attribute in the tag may not work due to these policies. To comply with browser restrictions and ensure that your audio plays, it’s recommended to initiate media playback as a response to a user action such as clicking a button.

    Login or Signup to reply.
  2. First you have a typo mistake ‘audi.play()’ should be ‘audioo.play’
    why don’t you try this

    <audio id="Audi">
      <source src="./RickRoll.mp3" type="audio/mpeg">
    </audio>
    
    <script>
        let audioo = document.getElementById("audi");
        audioo.play();
    </script>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search