Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to increase audio loading speed using JavaScript?

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.

like image 907
Ketan Patil Avatar asked Jun 29 '15 05:06

Ketan Patil


1 Answers

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.

like image 101
Kaiido Avatar answered Nov 01 '22 11:11

Kaiido