Weird NetStream problem when using function
This code causes my f4v file to stop playing prematurely. Time changes but roughly 8-10 seconds in.
loadSong();
function loadSong()
{
if(!songPlaying)
{
songPlaying = true;
var customClient:Object = new Object();
customClient.onCu开发者_JAVA技巧ePoint = cuePointHandler;
customClient.onMetaData = metaDataHandler;
var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
ns.client = customClient;
ns.play("song.f4v");
}
trace("HERE");
}
function cuePointHandler(infoObject:Object):void{
trace(infoObject.name);
}
function metaDataHandler(infoObject:Object):void {
trace("metaData");
}
This code let's the f4v play till the end. WTF!? It seems that when I call it via a function it causes the issue. FYI the code is stored in the first frame of the main timeline, and the F4v is audio only. Any help would be appreciated.
if(!songPlaying)
{
songPlaying = true;
var customClient:Object = new Object();
customClient.onCuePoint = cuePointHandler;
customClient.onMetaData = metaDataHandler;
var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
ns.client = customClient;
ns.play("song.f4v");
}
What is happening, when you declare your NetConection
and NetStream
inside the function, is that the scope for that variable is local to that function. That means that nothing else is referencing the NetConnection
you created and thus the garbage collector sweeps it up during its next run (that's why you are seeing the variable time).
When you declare it in just an if statement the variables are in the scope of the movie and that holds a reference to them and thus aren't garbage collected.
I don't know what the architecture is for the rest of your code, but if you want to use functions to hold your code, try putting a declaration for the var nc:NetConnection = new NetConnection();
just before the loadSong();
statement.
Architecturally, you might want to refactor your code out of the frame, but it might really not be worth it, if it is just couple of lines of code. Just depends on your project.
For more information about garbage collection, check out Understanding garbage collection in Flash Player 9 (It's says Flash Player 9, but this is applicable to 10 as well).
精彩评论