html - Javascript play and pause a song with one click on the same image - Stack Overflow

Hi im trying to have a song play when i click and pause when i click it again however it only seems to

Hi im trying to have a song play when i click and pause when i click it again however it only seems to be playing this is the code below:

Javascript

 function StartOrStop(audioFile)
  {
    var audie = document.getElementById("myAudio");
    if(!audie.src) 
       audie.src = audioFile;
    if(audie.paused == false)
     {
        audie.pause();
     }
     else
     {
       audie.play();
     }
  }

HTML5

<img src="images/play.png" alt="Play Button width="57" height="50" onclick="StartOrStop('RWY.mp3')" >

Any suggestion on how i can fix it ?

Hi im trying to have a song play when i click and pause when i click it again however it only seems to be playing this is the code below:

Javascript

 function StartOrStop(audioFile)
  {
    var audie = document.getElementById("myAudio");
    if(!audie.src) 
       audie.src = audioFile;
    if(audie.paused == false)
     {
        audie.pause();
     }
     else
     {
       audie.play();
     }
  }

HTML5

<img src="images/play.png" alt="Play Button width="57" height="50" onclick="StartOrStop('RWY.mp3')" >

Any suggestion on how i can fix it ?

Share Improve this question edited Mar 23, 2013 at 20:03 user2202031 asked Mar 23, 2013 at 12:33 user2202031user2202031 251 gold badge1 silver badge7 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 2

When ever you click the image, it calls the StartOrStop function, this will cause the src of video set again thus cause paused to be true so you can't pause the video but just play it again.

This version should work:

HTML:

<img src="images/play.png" alt="Play Button" width="57" height="50" onclick="StartOrStop('RWY.mp3')">

<audio id="myAudio"></audio>

Javascript:

function StartOrStop(audioFile) {
    var audie = document.getElementById("myAudio");
    if (!audie.src || audie.src !== audioFile) audie.src = audioFile; // check if there's a src already and if the current src is not the same with the new one, change it. Or don't do anything.
    if (audie.paused == false)
        audie.pause();
    else
        audie.play();
}

Fiddle: http://jsfiddle/QCGYP/4/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745496684a4630221.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信