JQuery: How do you download sound?

I have a website that plays sound at the click of a button.

This is how i do it

function PlaySound(soundfile) { $('#soundContainer').html("<embed src='/uploads/sounds/" + soundfile + "' hidden=true autostart=true loop=false>"); } 

How did you preload the sound file in advance?

Thank.

+9
jquery audio preload
May 4 '10 at 4:46
source share
5 answers

just thinking at the top of my head, you can make an ajax request for the sound file, but don't show the button until the file is 100% loaded.

 $(document).ready(function() { $.ajax({ url: "soundfile.mp3", success: function() { $("#play_button").show(); } }); }); 
+12
May 04 '10 at 5:54 a.m.
source share

There are so many better ways to play sound using JavaScript. For my beloved, check out SoundManager . With it, you can simply call soundManager.load to download the sound file proactively.

+2
May 4 '10 at 4:59
source share

Do it in HTML5 as follows:

 my_sound = $("<audio>", {src:"filename.mp3",preload:"auto"}).appendTo("body"); 

Then, to play it:

 my_sound[0].play(); 
+1
Sep 02 '15 at 3:07 on
source share
  • Make an AJAX request. If your server issues the correct caching headers (e.g. Expires), it will be stored in the browser cache.

  • Ugly way:

  soundfile = new Image (); 
 soundfile.src = / uploads / sounds / "+ soundfile; 
0
May 04 '10 at 4:58
source share

There is no such thing as <embed> .

You can use the HTML5 <audio> element without autoplay , and then start playback using the play() method.

0
May 04 '10 at 4:58
source share



All Articles