简体   繁体   中英

how to restart an mp3 file in javascript

I am having trouble restarting an mp3 file. I have the following event handler set to trigger when the space key is pressed. I also have an audio tag element stored in aud :

var isPlaying=false;
var h=function(e){
  console.log('confirm event has triggered');
  if(e.keyCode==32){
    if(isPlaying){
      aud.pause();
      aud.currentTime=0;
      isPlaying=false;
    }else{
      aud.play();
      isPlaying=true;
    }
  }
};

The first time I press space the audio begins playing just fine. The second time I press space the audio pauses as expected. The third time I press space I am now expecting the audio to restart from the beginning, but instead it continues from the point that it paused. It is as if currentTime is unsupported. I am using chrome.

It turns out the problem was that my node server did not serve byte ranges. Apparently chrome ignores certain playback features unless the server is capable in this way.

I had this problem and a small change worked for me:

this.currentTime = 0;

instead of

 aud.currentTime=0;

My original source for this tip was: http://bioinfornetics.blogspot.com/2011/02/how-to-resetrestart-position-of-html5.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM