dA_uNknOwN dA_uNknOwN - 1 month ago 16
Javascript Question

Javascript Audio Play on click

I have a javascript code to start a sound on click .
It works on chrome but on fireforx it startes onload ,but i want it onclick theire too.
Can anyone help?

<script>
var audio = new Audio("http://music.ogg" ) ;

audio.oncanplaythrough = function(){
audio.play();
}

audio.loop = true;

audio.onended = function(){
audio.play();
}

</script>

<input type="image" src="http://button.png" onclick="audio.play()">

Answer

Try this

  <!DOCTYPE HTML>
  <html>
  <head>
  <title>Audio</title>
  </head>
  <body>

    <script>
  function play(){
       var audio = document.getElementById("audio");
       audio.play();
                 }
   </script>

<input type="button" value="PLAY"  onclick="play()">
<audio id="audio" src="http://dev.interactive-creation-works.net/1/1.ogg" ></audio>
 </body>
 </html>