0

Trying to add a sound to a button, can´t figure out what´s wrong. Is there any other ways to do it?

HTML

<audio id="myAudio">
   <source href="url"="http://lab.wijkmanska.se/webbteknik/te13/mattias_martinsson/V08/KnappMattias (1).wav" type="audio/wav">
</audio>
<button type="button" onclick="aud_play_pause()">Play/Pause</button>

JavaScript

function aud_play_pause() {
  var myAudio = document.getElementById("myAudio");
  if (myAudio.paused) {
    myAudio.play();
  } else {
    myAudio.pause();
  }
}
  • This isn't valid `href="url"="http://lab.wijkmanska.se/webbteknik/te13/mattias_martinsson/V08/KnappMattias (1).wav"` – j08691 Feb 22 '16 at 15:39
  • 1
    An answer to this question has been made here: http://stackoverflow.com/questions/18826147/javascript-audio-play-on-click – c00ki3s Feb 22 '16 at 15:42

2 Answers2

1

Working example over here.

var playBtn = document.getElementById('play');
var stopBtn = document.getElementById('stop');

var playSound = function() {
audio.play();
};

playBtn.addEventListener('click', playSound, false);
stopBtn.addEventListener('click', function(){audio.pause()}, false);

http://jsfiddle.net/dsuket/jTh3v/

You need to change your audio path since it's invalid.

edit: I changed the audio path for you. Fiddle updated with your sound.

Mand
  • 499
  • 1
  • 5
  • 25
1

The simple fix to your example is to correct the source tag to src="path"

<source src="http://lab.wijkmanska.se/webbteknik/te13/mattias_martinsson/V08/KnappMattias (1).wav" type="audio/wav">
Andy Arndt
  • 385
  • 1
  • 7