Loading a SET of audio files

Hi all,

I have been looking at some forums and the net in general to see if someone ever had a similar problem but this one is just a bit different.

What I have is 2 different sets of sound files ( each set contains say 10 unique sound files) that I wish to load dynamically depending upon the user choice. So if I have SET A and SET B with 10 unique sound files each, and the user chooses SET B ( say via a Button), then i wish to load and embed those 10 sound files in my program, As of now I am using the flash 3 library to simple embed a single SET of files.

While there were quite a few examples of loading single files, I wonder if there is a way to do so for a set of them.

Thanks loads,
Ajoo.

That might depend on how you’re playing them. When you play them, are you loading them on demand - at the point in which they are played? Or do you preload all your sound files at once and pull from that selection.

Actually, either way, it might be as simple as using a variable to specify a distinguishing character(s) in the path name of your files. Then, when your files are referenced (either together, or individually) you just use that variable to build your path.

var selectedSoundSet = "seta";
var soundToPlay = "";

function getSoundPath (soundName) {
   return "sounds/sets/" + selectedSoundSet + "/" + soundName + ".mp3";
}

soundToPlay = getSoundPath("guitar");
// sounds/sets/seta/guitar.mp3

selectedSoundSet = "setb";

soundToPlay = getSoundPath("guitar");
// sounds/sets/setb/guitar.mp3

Hi senocular, Thanks for the reply. I kind of get the idea. Thanks for the code example because that’s how I better understand.

Right now I have simply one set which is in the library and which is published. I do not have to bother about the any path. Yes I would like to preload the entire set and pull from the selection. While it is clear to me from your example above how the sound files would be loaded it is not clear to me if that example would also ‘embed’ the entire set all at once.

I would like to embed and entire set at the beginning ( preload) and then pull out sounds without the hassel of paths like I am doing with the sounds in my code. (I think the publisher takes care of the paths in my case). I would be glad if you can show me how I may be able to accomplish that with the sets of sounds.

Thank you very much.

That’s just a matter of batch loading files. When you’re loading externally at runtime, they’re not really “embedded” at that point. That term is primarily reserved for what you do before you create your SWF and what is added to the SWF itself as part of that file - what’s embedded in that file.

You should be able to find a bunch online about batch loading files in Flash. I think people like using https://greensock.com/LoaderMax-AS3 more than anything else. Its a good place to start. Its a library that simplifies the process of loading multiple files at once rather than doing it yourself.

Hi Senocular,

Thanks for that information. Will read, try out and revert if that solved my issue.

Thanks very much.

Hi Senocular,

I have been through the suggested link and since I have no experience using libraries such as greensock, I find it very heavy. Isn’t there a simple way to do it in just flash? I wouldn’t mind using the earlier method you suggested using different paths if that would solve my problem. Please kindly suggest some alternative method to tackle this one.

Thanks very much.

Simple? Hmmm. LoaderMax exists because doing this is not simple :wink:

The idea is to keep a list of things you want to load and track the loading of those things until all of those things in that list are all loaded, then move on to the next thing in your program, whatever that may be.

There are a number of different ways to keep track of the things in your list, for example counting the number of completed loads, maintaining different lists for downloading and completed items, or looping through each item checking for completion every time any item completes, etc. The counter approach is probably the simplest. Its not unlike a for or while loop counter. Once the counter reaches the end, the “loop” is done and you move on with your life.

One of the worst parts of this is error handling and managing listeners. For example, see the simple, one-item example at the very bottom of the page here:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/URLLoader.html

Here’s some pseudo code for a very basic case. Don’t expect this to work as is, just give you an idea on how it would:

var loaders = []; // list of loaders for each of your files
var loadCounter = 0; // counts load completes as they come in
startLoading(["a.mp3","b.mp3","c.mp3"]); // start process

function startLoading(urls) {
   for (var i=0, n=urls.length; i<n; i++) {
      var loader = new URLLoader();
      loaders.push(loader);
      loader.addEventListener(Event.COMPLETE, loadComplete);
      loader.load(new URLRequest(urls[i]);
   }
}
function loadComplete(event) {
   loadCounter++;
   if (loadCounter >= loaders.length) {
      // everything complete, dispatch an event
      // or call a function to continue with app
   }
}

Hi Senocular,

Thanks once again for such a clear reply. So this is not a simple process. That should prepare me to look at all options then. I will look at this example as well as the greensock once again now.

I would however like to clarify one thing though. What’s happening, in terms of resource loading, in my case at present? I mean as of now there is just one set of audio files and they are in the library and when I publish the swf the resource is already available. So does that mean that in this case it has already been preloaded by flash? I now need to use the loader class because I want to change or switch between that and another set of audio files ? Is that it or am I missing something still?

Would be glad for that clarification. Would make things clear in my head !

Thanks loads again !

This is: senocular.com

When you store audio and such in the Flash library, it’s generally loaded along with the contents of the first frame of the movie, so that audio is available to the rest of your code, pretty much wherever it runs.

Depending on what you mean, sure. As soon as your browser (or standalone player) is instructed to load your SWF file, your sounds will be among the first things loaded, and you won’t have to write code to load those sounds.

That’s pretty much it. You could include both sets of sounds ahead of time, and give each set a different name you could use to distinguish them on a button press. This just means that the SWF file size would be larger, because it has both audio sets. If a file size increase is unacceptable to you, then you must use a dynamic loading technique like senocular’s pseudocode or LoaderMax.

Hi Krilnon,

Thanks for that explanation. I am pretty clear now.
Thanks very much.

1 Like