Pratish Shrestha
Pratish Shrestha

Reputation: 1892

How do I play same sound again by pausing the previous instance?

I have a sound instance that plays when the player collides with an object.

  var coin = new Audio('sounds/coin.wav');
  if(type == 2){  //Coin Box
    coin.play();
  }

The problem is , when the player collides with multiple objects of the same kind, the sound doesnt play again as it is already playing. How do i stop the previous sound and play it again? also without having to create another instance of the same sound.

Something like this.

    if(type == 2){ 
      coin.pause();
      coin.play();
    }

Upvotes: 0

Views: 34

Answers (1)

epascarello
epascarello

Reputation: 207501

Set the time back to the start.

coin.pause();
coin.currentTime = 0;
coin.play();

Upvotes: 2

Related Questions