7

I have created code for playing an .mp3 file using JavaScript. But the file is taking to much time to play on Android. I want to play the sound quickly after click on a text image. How do I increase loading speed on Android?

My JavaScript code:

if(window.audio) 
{
    audio.pause(); 
}
window.audio = new Audio (audio_path);
window.audio.play();

Check demo here - http://97.74.195.122/gizmo-demo/canvasslidertwo.php

In the above link, click on the Japanese word for playing sound.

ZygD
  • 22,092
  • 39
  • 79
  • 102
  • possible duplicate of [Cloning audio source without having to download it again](http://stackoverflow.com/questions/30433667/cloning-audio-source-without-having-to-download-it-again) – Kaiido Jun 29 '15 at 06:12

1 Answers1

3

You are actually facing the bug depicted in this question. Which makes chrome browsers redownload the files from an Audio Element each time you change its src property (no-caching).

One solution would be to use the WebAudioAPIand store your audio files in the buffer. Doing so, you should be able to call them with no latency. You can check the accepted answer for an example on how to deal with it.

Or you could also try not to call a new Audio() on each click, and rather load every audios from your page and only call their play() method on click. But I'm not sure it would fix your issue.

Community
  • 1
  • 1
Kaiido
  • 123,334
  • 13
  • 219
  • 285