Search code examples
javascriptfunctionaudio

When I click a button my audio won't play


I made a button and then made it so it sends playAudio() on click of itself. self.

I tried to get it to send a message to the function to do it, and I think that works fine. I think JavaScript is the part that doesn't work.

<body>
    <button class="playAudio" onclick="playAudio()">Play audio</button>
</body>
const audio = new Audio("C:UsersjbrirDocumentsBowgartWebsite2audioBowgart_sample.mp3e");

function playAudio() {
  audio.play();
}

I expected it to play the audio, but it ended up playing nothing.


Solution

  • I think you have the issue in the path.

    Please try to do like this, C:/.../file.mp3

    code.

    <button class="playAudio" onclick="playAudio()">Play audio</button>
    
    <script>const audio = new Audio(
        "C:/Users/jbrirDocumentsBowgartWebsite2audioBowgart_sample.mp3"
      );
      
      function playAudio() {
        audio.play();
      }</script>