Click here to Skip to main content
15,917,926 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a page for which i need to play a sound (audio tag or js Audio object) at a specific instance. The audio file location is not in my local webserver, it's at another server accessed like 'https://site.com/folder/file.wav'

I need to access and load this file at page load and need to play it without having a delay. Adding my current code below

JavaScript
<script> 
 var player = new Audio();

 $(document).ready(function () {
   player.src = 'https://site.com/folder/file.wav';
 });

function play()
{
player.play();
}
</script>


Problem i'm facing now is, if i'm running it from a computer with WIFI, it plays ontime; but if i run that from my android mobile it plays after 3-5 seconds.

Please advise.
Posted

1 solution

That 3-5 seconds is the time the mobile device have to download your music from the server. The problem is that HTML5 audio/video tag starts to download it's target file only after the page downloaded and the tag created in the DOM. Even with autoplay on, you can't do nothing with that...
 
Share this answer
 
Comments
DieOnTime 20-May-14 7:49am    
I'm ok with the 3-5 seconds, but is there any chance i can have this downloaded and play it on a function call anytime later?

Thanks in advance
Kornfeld Eliyahu Peter 20-May-14 7:52am    
Yes. You can use the preload attribute of audio/video elements...
http://www.w3.org/wiki/HTML/Elements/video
DieOnTime 20-May-14 7:57am    
tried that already. still it takes a few seconds from play() invoke on mobile. :(
Kornfeld Eliyahu Peter 20-May-14 8:02am    
There will be a certain delay from the beginning of the download until it will be responsive...
In theory you can download and store the file in local storage, but I doubt if there is enough room for that...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900