开发者

How to stop audio played by audio tag of HTML5

I am new to HTML5 and I am exploring HTML5 features. There I came across audio tag.

I written code with it to play sound files.

I am having one problem with this tag. On button click, I want to change the sound file stopping the previous one.

I searched a lot but didn't find anything. P开发者_Go百科lease help me to stop the sound played by audio tag.

Here is my code;

  try
  {
       if(MyAudio != undefined)
       {
          MyAudio = null;
       }
       MyAudio = new Audio("filename");
       MyAudio.play();
  }
  catch(v)
  {
    //alert(v.message);
  }


Try this:

  try
  {
       if(MyAudio != undefined)
       {
          MyAudio.pause();
       }
       MyAudio = new Audio("filename");
       MyAudio.play();
  }
  catch(v)
  {
    //alert(v.message);
  }


As commented in this cuestión: HTML5 Audio stop function, a better solution could be:

var sound = document.getElementById("audio");
sound.pause();
sound.currentTime = 0;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜