ActionScript 2.0 NetStream Annoyance

Here’s a “heads up” to anyone who may be pulling his/her hair out wondering why NetStream won’t load an FLV. For whatever reason (I’m guessing it’s a bug), if you have a strongly-typed NetStream within a function, it will not work.

For example, the following code will work on “_root”:

var my_video:Video; // my_video is a Video object on the Stage
var my_nc:NetConnection = new NetConnection();
my_nc.connect(null);
var my_ns:NetStream = new NetStream(my_nc);
my_video.attachVideo(my_ns);
my_ns.play("video1.flv");

Try dropping this into a function or class and you will see that it does not work at all. To make it work, you must completely remove the data typing and variable declaration for the NetStream and NetConnection:

var my_video:Video; // my_video is a Video object on the Stage
my_nc = new NetConnection();
my_nc.connect(null);
my_ns = new NetStream(my_nc);
my_video.attachVideo(my_ns);
my_ns.play("video1.flv");

See Adobe’s LiveDocs on this topic for more information.

I wasted far too many hours trying to figure this out thinking that the problem was with my code. I hope I can save someone else from the same frustration.