Jump to content
GreenSock

Search In
  • More options...
Find results that contain...
Find results in...
stevenp

How to remove a loader and loaded content?

Recommended Posts

Thank you in advance for reading. I want to get my head around LoaderMax, and to that end I want to manually destroy a loader and the content it loads. This loads an array of MP3s and plays them:

var queue:LoaderMax;
var sound:MP3Loader;
var positionOfSong:int;
var preloadCount:uint = 0;
var curSoundIndex:int = -1;

var mp3Array:Array = ["01.mp3", "02.mp3", "03.mp3" ];

initPreload();

function initPreload():void {
	queue = new LoaderMax({name:"mainQueue", onChildComplete:childCompleteHandler, onError:errorHandler, noCache:true, maxConnections:1, dispose:true});
	for (var i:int = 0; i < mp3Array.length; i++) {
		queue.append(new MP3Loader(mp3Array[i], {name:"mp3"+i, autoPlay:false}));
	}
	queue.load(true);
}

function errorHandler(event:LoaderEvent):void {
	trace("Error occured with " + event.target + ":" + event.text);
}

// make sure one song is loaded before playing back
function childCompleteHandler(event:LoaderEvent):void {
	preloadCount++;
	if (preloadCount == 1) {
		playAudioFile(0);
	}
}

How do I reference and remove the loaders and the loaded content?

 

thanks,

Steve

Link to comment
Share on other sites

Let me refine, and ask "How I would reference the loader and content from a seperate function or listener?"; one that is not related to onComplete or onError, for example.

Link to comment
Share on other sites

Thanks Carl.

 

1) In my above code, what is the name of my loader?

2) Once I've identified that name, I don't get the way to flush it.

3) What does my line queue.load(true); do?

 

I assume from my code above the name is "mainQueue" (makes sense to me):

LoaderMax.getLoader("mainQueue");

 

If that is the correct name ... what next? In another function ... do I do:

 

LoaderMax.getLoader("mainQueue").dispose() ?

 

I'm sure it is obvious ... but not to me. I don't come from an object oriented background, I come from a design background. It's like ... I need documentation to understand the documentation, and that's embarrassing to admit.

 

The goal is I want to destroy the loader and it's content in the hopes that I can then play the queue a second time (behaves just fine the first time through, but will not play a second time).

 

Thanks,

Steve

Link to comment
Share on other sites

I tried LoaderMax.getLoader("mainQueue").dispose(); and while I didn't get an error, it didn't solve my problem (nor do I know if "unload" is what I want to do, for that matter).

 

Does dispose also remove the loaded assets? Is there a way to list loaded assets? Using CS4 I can't see an obvious way to enumerate loaded assets ("List objects" and "List variables" does not).

 

Thanks,

Steve

Link to comment
Share on other sites

Well, using the "status" I'm able to determine that the loader is unloaded ... but does that also mean the assets are unloaded?

 

And if unloaded, shouldn't the function that loads and plays the assets actually work on the second iteration?

 

I am not able to upload a file because the smallest I can make it is 7MB.

 

So, I guess I'll regurgitate the code here:

import com.greensock.*;
import com.greensock.easing.*;
import com.greensock.plugins.*;
import com.greensock.loading.*;
import com.greensock.loading.display.*;
import com.greensock.events.LoaderEvent;
import com.greensock.events.*;
import flash.events.MouseEvent;

LoaderMax.activate([MP3Loader]);
//////////////////////////////////////////////////////////////////////
var playInteger:int = 0;
//////////////////////////////////////////////////////////////////////
var queue:LoaderMax;
var sound:MP3Loader;        // declare outside of any function
var positionOfSong:int;
var preloadCount:uint = 0;
var curSoundIndex:int = -1;
var mp3Array:Array = ["1.mp3", "2.mp3", "3.mp3"];
///////////////////////////////////////////////////////////////////////
// listeners
mc_00.addEventListener(MouseEvent.CLICK, functionPlay00);
mc_01.addEventListener(MouseEvent.CLICK, functionPlay01);
mc_02.addEventListener(MouseEvent.CLICK, functionPlay02);
mc_03.addEventListener(MouseEvent.CLICK, functionPlay03);
btn_play2.addEventListener(MouseEvent.CLICK, functionPlayButton);
btn_play2.addEventListener(MouseEvent.CLICK, functionResumeSongButton);
btn_pause2.addEventListener(MouseEvent.CLICK, functionPauseButton);
btn_next2.addEventListener(MouseEvent.CLICK, functionNextSongButton);
btn_previous2.addEventListener(MouseEvent.CLICK, functionPreviousSongButton);
///////////////////////////////////////////////////////////////////////
// functions
function initPreload():void {

    queue = new LoaderMax({name:"mainQueue", onChildComplete:childCompleteHandler, maxConnections:1});

    for (var i:int = 0; i < mp3Array.length; i++) {
        queue.append(new MP3Loader(mp3Array[i], {name:"mp3"+i, autoPlay:false}));
        
    }

    queue.load();
}

// make sure one song is loaded before playing back
function childCompleteHandler(event:LoaderEvent):void {
    preloadCount++;
    if (preloadCount == 1) {
        playAudioFile(0);
    }
}

// original 'play' function
function playAudioFile(index:uint):void {
if (curSoundIndex != -1) {
        var old:MP3Loader = LoaderMax.getLoader(mp3Array[curSoundIndex]) as MP3Loader;
        old.removeEventListener(MP3Loader.SOUND_COMPLETE, soundCompleteHandler);
        TweenLite.to(old, 1, {volume:0, onComplete:old.pauseSound});
    }
    curSoundIndex = index;
    
    sound = LoaderMax.getLoader(mp3Array[index]) as MP3Loader;

    sound.addEventListener(MP3Loader.SOUND_COMPLETE, soundCompleteHandler);
    sound.gotoSoundTime(0, true);
    TweenLite.to(sound, 1, {volume:1});
    trace(mp3Array[curSoundIndex]); // the current playing mp3 file
    
    var url:String = mp3Array[curSoundIndex];
    var request:URLRequest = new URLRequest(url);
    var mp3:Sound = new Sound(request);
    mp3.addEventListener(Event.ID3, id3Handler)
}

function soundCompleteHandler(event:LoaderEvent):void {
    var nextIndex:int = curSoundIndex + 1;
    trace(nextIndex);
    if (nextIndex >= mp3Array.length) {
        nextIndex = 0;
    }
    playAudioFile(nextIndex);
}

// button functions
function functionPlay00(e:MouseEvent):void{
flash.media.SoundMixer.stopAll();
    var curSoundIndex:int = -1;
    playInteger = playInteger + 1;
    trace("Play All");
    trace("Button click number " + playInteger);
    initPreload();
}

function functionPlay01(e:MouseEvent):void{
flash.media.SoundMixer.stopAll();
var sound:MP3Loader = new MP3Loader("1.mp3", {name:"audio", autoPlay:true, estimatedBytes:9500});
var url:String = "1.mp3";
var request:URLRequest = new URLRequest(url);
var mp3:Sound = new Sound(request);
mp3.addEventListener(Event.ID3, id3Handler)
sound.load();
    }

function functionPlay02(e:MouseEvent):void{
flash.media.SoundMixer.stopAll();
var sound:MP3Loader = new MP3Loader("2.mp3", {name:"audio", autoPlay:true, estimatedBytes:9500});
var url:String = "2.mp3";
var request:URLRequest = new URLRequest(url);
var mp3:Sound = new Sound(request);
mp3.addEventListener(Event.ID3, id3Handler)
sound.load();
    }

function functionPlay03(e:MouseEvent):void{
flash.media.SoundMixer.stopAll();
var sound:MP3Loader = new MP3Loader("3.mp3", {name:"audio", autoPlay:true, estimatedBytes:9500});
var url:String = "3.mp3";
var request:URLRequest = new URLRequest(url);
var mp3:Sound = new Sound(request);
mp3.addEventListener(Event.ID3, id3Handler)
sound.load();
    }

//////////////////////////////////////////////////////////////////////////////
// transport functions

function functionPlayButton(e:MouseEvent):void{
    trace("play button");
    }

function functionNextSongButton(e:MouseEvent):void{
    trace("next button");
    var nextIndex:int = curSoundIndex + 1;
    if (nextIndex >= mp3Array.length) {
        nextIndex = 0;
        }
        playAudioFile(nextIndex);
    }    

function functionPreviousSongButton(e:MouseEvent):void{
    trace("previous button");
    trace("Current Sound index: " + curSoundIndex);
    var nextIndex:int = curSoundIndex - 1;
    if (nextIndex <= -1) {
        nextIndex = mp3Array.length - 1;
        }
        playAudioFile(nextIndex);
    }    

function functionPauseButton(e:MouseEvent):void{
    trace("pause button");
    positionOfSong = sound.soundTime;
    trace("positionOfSong at pause time: " + sound.soundTime);
    sound.pauseSound();
    }    

function functionResumeSongButton(e:MouseEvent):void{
    trace("resume button");
    trace("positionOfSong at resume time: " + sound.soundTime);
    sound.gotoSoundTime(positionOfSong, true);
    }    

function functionStopButton(e:MouseEvent):void{
    trace("stop button");
    }    
////////////////////////////////////////////////////
// ID3 handling

function id3Handler(event:Event):void {
var song:Sound = Sound(event.target);
var songInfo:ID3Info = ID3Info(song.id3);
dyn_txt.htmlText="track: " + songInfo.songName + "<br />" + songInfo.comment;
}
////////////////////////////////////////////////////
Link to comment
Share on other sites

Hi,

 

I'm having trouble keeping up with your questions, it seems we are in different time-zones.

 

load(true) should load a loader and flush/unload its content first.

 

Using dispose(true) basically kills the loader, gets it ready for garbage collection and the true parameter also flushes and unloads the content of that loader.

 

I would suggest using:

 

LoaderMax.getLoader("mainQueue").dispose(true)

 

I notice you are adding eventListeners in some places like:

 

sound = LoaderMax.getLoader(mp3Array[index]) as MP3Loader;
sound.addEventListener(MP3Loader.SOUND_COMPLETE, soundCompleteHandler);

in order for sound to be disposed you would have to first remove that eventListener.

 

Also noticed you are using a LoaderMax which contains a bunch of MP3Loaders like so:

 

  for (var i:int = 0; i < mp3Array.length; i++) {
        queue.append(new MP3Loader(mp3Array[i], {name:"mp3"+i, autoPlay:false}));
        
    }

but then you create other MP3Loaders in these functions:

 

function functionPlay01(e:MouseEvent):void{
flash.media.SoundMixer.stopAll();
var sound:MP3Loader = new MP3Loader("1.mp3", {name:"audio", autoPlay:true, estimatedBytes:9500});
var url:String = "1.mp3";
var request:URLRequest = new URLRequest(url);
var mp3:Sound = new Sound(request);
mp3.addEventListener(Event.ID3, id3Handler)
sound.load();
    }

and in the same function you are creating a Sound object using the same url as MP3Loader.

 

In other words you seem to have a LoaderMax containing a bunch MP3Loaders, and then these "loose" ones that are being created in different functions. I'm not really understanding what is happening.

 

If there is a way you could simplify the code and focus on 1 feature of LoaderMax, like disposing or unloading loaders, that would be helpful. 

Link to comment
Share on other sites

Thanks Carl. I appreciate you telling me I'm all over the road. Believe it or not, that helps.

 

To answer one question: I wanted to build a player where a user could play an array of files, or a single file.

 

I'll let this job go and try to start with simple "Hello World" stuff.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×