Emphram Stavanger
Emphram Stavanger

Reputation: 4214

Preloading audio files to be used by jPlayer

I'm using (a hidden by width: 0;) jPlayer to play many short sound files intermittently.

I use .jPlayer("clearMedia") and .jPlayer("setMedia") to switch between the sound files whenever necessary.

However, this means the file is only loaded on play. Which causes a significant delay. I do though know that once the files are loaded, they are cached, and will play instantaneously when prompted again later.

Is there a way to load these audio files (each provided in both ogg and m4a) onload without autoplaying them, and in such a way that jPlayer would be able to make use of that cache later? I have a full JS array list of the sound files.

Upvotes: 2

Views: 1036

Answers (1)

jwhit
jwhit

Reputation: 41

You could use multiple jPlayers. Create a div for each sound file and then assign a jPlayer to each one. Something like: HTML:

<div id="sound1" class="audio" path="audiourl1"></div>
<div id="sound2" class="audio" path="audiourl2"></div>

and the Javascript/Jquery

$(".audio").each(function(){
    var $this = $(this);
    var path = $this.attr("path");
    $this.jPlayer({
        ready: function () {
          $(this).jPlayer("setMedia", {
            m4a: "/mp3/"+path+".m4a", // Defines the m4a (AAC) url
            oga: "/ogg/"+path+".ogg" // Defines the counterpart oga url
          });
        },
        supplied: "m4a, oga",
        swfPath: "/jPlayer/js"
    });
});

Upvotes: 4

Related Questions