Home > Mobile >  When I click a button my audio won't play
When I click a button my audio won't play

Time:12-24

I made a button and then made it so it sends playAudio() on a 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 the JavaScript is the part that doesn't work.

  <body>
        <button  onclick="playAudio()">Play audio</button>
  </body>
  <script>const audio = new Audio(
    "C:UsersjbrirDocumentsBowgartWebsite2audioBowgart_sample.mp3e"
  );
  
  function playAudio() {
    audio.play();
  }</script>

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

CodePudding user response:

I think you have the issue in the path.

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

code.

<button  onclick="playAudio()">Play audio</button>

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