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.
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.
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.
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.
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.
Simple? Hmmm. LoaderMax exists because doing this is not simple
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.
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
}
}
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 !
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.