1

This is how my script looks like:

<script>
    var obj = document.createElement("audio");
    document.body.appendChild(obj);
    obj.src = "http://example.com/audios/Kalimba.mp3";
    obj.load();
    obj.volume = 0.3;
    obj.preLoad = true;
    obj.controls = true;

    $(".testspeech").click(function() {
    obj.paused?obj.play():obj.pause()
    });
</script>

Then in the HTML I have a button that is meant to play and/or pause the video.

However, when I load the page nothing happens when I click on the play button.

Anything I could have possibly done wrong here?

Here is the HTML:

<button class="testspeech">
            play/pause
</button>
Sidney Sousa
  • 3,378
  • 11
  • 48
  • 99

1 Answers1

2

As far as I can tell the reason your sound isn't playing is that the src for the actual sound is not valid. I've created a JSFiddle (here) to test it out, and after replacing the sound clip link it works as expected.

$(document).ready(function() {
  var obj = document.createElement("audio");
  document.body.appendChild(obj);

  obj.src = "https://www.kozco.com/tech/piano2.wav";
  obj.load();
  obj.volume = 0.3;
  obj.preLoad = true;
  obj.controls = true;

  $(".testspeech").on('click', function() {
      obj.paused ? obj.play() : obj.pause()
    });
});
BornaS
  • 91
  • 4