Hi all,

This is my first time posting here, so it's slightly nerve-wracking, but I'll try my best to keep it simple and clear.

What I've been doing for the past few weeks is working with the YouTube AS3 API player (See here for more info: https://developers.google.com/youtub..._api_reference). It loads nicely, does everything I need it too, and is overall what I want. The only problem is that unloading seems to be an impossible task.

From what I can tell when I'm in Flash's debug mode, the player is loading in SWFs for the player to work. What it doesn't do is unload these SWFs, even when I use the inbuilt "destroy" function that YouTube has said will destroy the player.

This thing has definitely gotten me confused, and I've tried things such as unloadAndStop() on the loader object, making objects null when I'm done with them and removing all event listeners. To me it seems once the SWFs are loaded in, I have no control over them (and have no idea how I would even attempt to talk to them and tell them to go away).

I've attached a Flash CS4 FLA with my code in the timeline (keeping it simple). The file contains the player, along with two buttons for loading and unloading (which I have built in to work with the player's loading and unloading functions). I also put in a memory reader up the top to show how, when you load and unload fairly quickly, the memory starts to rise and eventually the videos start to become chunky and load slower.

The file is here: youtubePlayer.zip

And below is the code for anyone who wants to quickly skim through:

Code:
/*-----------
The below code was copied from the YouTube AS3 API documentation,
and edited slightly by me to add native button functionality
https://developers.google.com/youtube/flash_api_reference

I have declared the player and loader objects globally.

I have wrapped the actual video load into a function so that I can attach it
to a button press of a MovieClip.
---------------*/

// The player SWF file on www.youtube.com needs to communicate with your host
// SWF file. Your code must call Security.allowDomain() to allow this
// communication.
Security.allowDomain("www.youtube.com");

// This will hold the API player instance once it is initialized.
var player:Object;
var loader:Loader;

//This function loads a new instance of the chromeless YouTube video player
function loadVideo(evt:MouseEvent):void{
	mcLoad.removeEventListener(MouseEvent.CLICK, loadVideo); //Remove the mouse click event for the load video button
	mcLoad.gotoAndStop(2); //"Disable" the load button
	mcUnload.gotoAndStop(1); //"Enable" the unload button
	mcUnload.addEventListener(MouseEvent.CLICK, unloadVideo, false, 0, true); //Add the mouse click event for the unload video button
	
	player = new Object();
	loader = new Loader();
	loader.contentLoaderInfo.addEventListener(Event.INIT, onLoaderInit, false, 0, true);
	loader.load(new URLRequest("http://www.youtube.com/apiplayer?version=3"));
}

//This function is "supposed" to unload the chromeless YouTube video player
function unloadVideo(evt:MouseEvent):void{
	mcUnload.removeEventListener(MouseEvent.CLICK, unloadVideo); //Remove the mouse click event for the uload video button
	mcUnload.gotoAndStop(2); //"Disable" the unload button
	mcLoad.gotoAndStop(1); //"Enable" the load button
	mcLoad.addEventListener(MouseEvent.CLICK, loadVideo, false, 0, true); //Add the mouse click event for the unload video button
	
	//Remove event listeners and unload the loader object (try catch to avoid referencing null loader)
	try {
		loader.contentLoaderInfo.removeEventListener(Event.INIT, onLoaderInit);
		loader.content.removeEventListener("onReady", onPlayerReady);
		loader.unloadAndStop();
	} catch(error:Error){
		trace("loader hasn't loaded yet");
	}
	
	//Stop the player video, then use the YouTube API destroy function (try catch to avoid referencing null player)
	try {
		player.stopVideo();
		player.destroy();
	} catch(error:Error){
		trace("player wasn't started yet");
	}
	
	//Set both objects to null
	loader = null;
	player = null;
}

//This function adds the loader to the stage and adds various state change events,
//such as when the quality or state (playing, paused) of the player changes
function onLoaderInit(event:Event):void {
    addChild(loader);
    loader.content.addEventListener("onReady", onPlayerReady, false, 0, true);
}

//This is the function that sets up the player to an object so that it can be controlled
//with code for positioning and playback
function onPlayerReady(event:Event):void {
    // Event.data contains the event parameter, which is the Player API ID 
    trace("player ready:", Object(event).data);

    // Once this event has been dispatched by the player, we can use
    // cueVideoById, loadVideoById, cueVideoByUrl and loadVideoByUrl
    // to load a particular YouTube video.
    player = loader.content;
    // Set appropriate player dimensions for your application
    player.setSize(640, 360);
	// Load a video into the player
	player.loadVideoById("GOMmvI2y4u8");
}


/*---------
THE BELOW CODE IS MY OWN, FOR THE LOAD AND UNLOAD BUTTONS
-----------*/

mcLoad.buttonMode = mcUnload.buttonMode = true;

mcUnload.gotoAndStop(2);
mcLoad.addEventListener(MouseEvent.CLICK, loadVideo, false, 0, true);

/*----------
This code attaches a simple memory test to the stage (so that it's above everything)
And displays the memory usage of the stage. You can see it rise exponentially
as you load and unload videos.

Running the SWF in debug mode also shows the 2 SWFs for the player being loaded, with
neither of them being fully unloaded (leaving the other SWFs on stage to eat up memory
and probably load videos in the background, causing lots of lag for loading other
videos later on)

The SWFS being loaded but not unloaded appear like this in the Output Panel in Debug:
[SWF] /apiplayer - 3599 bytes after decompression
[SWF] /yt/swfbin/apiplayer3-vflYsZyqY.swf - 416842 bytes after decompression

Neither of the above SWFs ever get unloaded
-----------*/

var memoryHolder:MovieClip = new MovieClip();
var memoryBg:MemoryBg = new MemoryBg();
var txtMemory:TextField = new TextField();
			
memoryBg.width = stage.stageWidth;
memoryBg.height = 50;
			
stage.addChild(memoryHolder);
memoryHolder.addChild(memoryBg);
memoryHolder.addChild(txtMemory);
			
stage.addEventListener(Event.ENTER_FRAME, displayMemory, false, 0, true);

//This function displays the memory fps and MB usage
function displayMemory(evt:Event):void{
	var mem:String = Number( System.totalMemory / 1024 / 1024 ).toFixed( 2 ) + 'Mb';
	txtMemory.text = mem;
}
In the end, if this doesn't work, it doesn't work. The main reasons for using this are that I don't have to do my own streaming, and don't need my own server for hosting videos. If the whole idea of using the YouTube AS3 API is just bad, please let me know so that I never poke it again with a 50 foot pole.

Any help at all, even a comment on the whole idea and whether or not I should bother with using this API, would be greatly appreciated. Thanks in advance!