javascript - Playing multiple one shot sound effects at the same time on mobile browsers? -


When I click on a button in my web app to the user, I am trying to run sound effects.

I tried this method for the first time:

  var sound = new audio ("soundeffect.ogg"); Function clickhandler () {sound.play (); }  

However, if the user presses the button twice in quick succession, the sound is played only once. I am assumed that sound play has not ended, and therefore has not been rewinded.

Then I tried this solution.

  var sound = new audio ("soundeffect.ogg"); Function clickhandler () {sound.currentTime = 0; Sound.play (); }  

However, before the next sound is played it causes the first sound to be cut.

I then tried this version, which causes a new sound effect every time a button is clicked:

  function clickhandler () {var sound = New audio ("soundeffect.ogg"); Sound.play (); }  

This works perfectly on my developer machine, but using the Android Chrome on mobile data, the sound effect is loaded every time it seems that the clicked button The sound is played, which may delay several seconds before the sound is played.

I was hoping to declare sound effects in the global scope, then cloning can help keep it in the cache, but it did not look helpful.

  Miscellaneous sound = new audio ("soundeffect.ogg"); Function clickhandler () {var oneShotSound = sound.cloneNode (); OneShotSound.play (); }  

I wrote a web audio library, Wad.js, which I think That will help you in this matter.

  var sound = new wall ({source: 'soundeffect.ogg'}); Sound.play ();  

Wad.js will call a new audio buffer source node every time you call play () , so that you hear many examples of the same sound But all the audio buffer source nodes use the same audio buffer, which saves you a lot of computational work.

Check it

However, cross-browser compatibility is not correct. I hope that it will work in Chrome for Android, but I have not tested it.


Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -