Multicast Streaming in Flash Player 10.1 revisited

Yesterday a student from my class ask me to help him solve a problem that was getting when testing Tom Krcha’s example files of his Multicast Streaming tutorial. Here’s the fix.

Yesterday a student from my class ask me to help him solve a problem that was getting when testing Tom Krcha’s example files of his Multicast Streaming tutorial, the files he was using were the same included in Tom’s tutorial except for the Stratus Developer Key so after looking at the video tutorial included and testing the examples files You’ll get this error:

?View Code ACTIONSCRIPT3
1
2
3
4
5
Error: Error #2154: The NetStream Object is invalid.  This may be due to a failed NetConnection.
	at flash.net::NetStream/invoke()
	at flash.net::NetStream/attachCamera()
	at Broadcaster/setupStream()[C:\Users\Dave\Adobe Flash Builder 4\Broadcaster\src\Broadcaster.mxml:67]
	at Broadcaster/netStatus()[C:\Users\Dave\Adobe Flash Builder 4\Broadcaster\src\Broadcaster.mxml:50]

The error is pretty descriptive and what is saying is that was the NetStream was not getting the Camera signal or received a null value when trying to attach it.

The reason that this was happening is because in the NetStatusEvent Listener inside the switch statement we are using the event.info.code = “NetConnection.Connect.Success”; both for the Connection and the Stream

?View Code ACTIONSCRIPT3
1
2
netConnection.addEventListener(NetStatusEvent.NET_STATUS, netStatus);
stream.addEventListener(NetStatusEvent.NET_STATUS, netStatus);
?View Code ACTIONSCRIPT3
1
2
3
4
5
6
7
8
9
10
private function netStatus(event:NetStatusEvent):void{
	   writeText(event.info.code);
 
	   switch(event.info.code){
	            case "NetConnection.Connect.Success":
		    setupStream();
                    break;
		}
 
}

And on the setupStream() function we are trying to attach the camera and thats what throws the error cause we are using the “NetConnection.Connect.Success” to call this function.

?View Code ACTIONSCRIPT3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
private function setupStream():void
{
      var groupspec:GroupSpecifier = new GroupSpecifier("myGroup/multicastOne");
      groupspec.serverChannelEnabled = true;
      groupspec.multicastEnabled = true;
 
      stream = new NetStream(netConnection,groupspec.groupspecWithAuthorizations());
      stream.addEventListener(NetStatusEvent.NET_STATUS, netStatus);
 
       var cam:Camera = Camera.getCamera();
 
       stream.attachCamera(cam);
       stream.publish("multicast");
 
	video.attachCamera(cam);
}

The fix is pretty straightforward all You have to do is include a case for the event.info.code: “NetStream.Connect.Success” and thats where we attach the camera to our stream.

?View Code ACTIONSCRIPT3
1
2
3
4
5
6
7
8
9
10
11
switch(event.info.code){
	case "NetConnection.Connect.Success":
	        setupStream();
	break;
	case "NetStream.Connect.Success":
	        var cam:Camera = Camera.getCamera();
	        stream.attachCamera(cam);
	        stream.publish("multicast");
	        video.attachCamera(cam);
	break;
}

And our setupStream() function will only define the GroupSpecifier and the NetStream:

?View Code ACTIONSCRIPT3
1
2
3
4
5
6
7
8
private function setupStream():void{	
	var groupspec:GroupSpecifier = new GroupSpecifier("myGroup/multicastOne");
	groupspec.serverChannelEnabled = true;
	groupspec.multicastEnabled = true;
 
	stream = new NetStream(netConnection,groupspec.groupspecWithAuthorizations());
	stream.addEventListener(NetStatusEvent.NET_STATUS, netStatus);
}

Attaching camera / microphone after NetStream is connected, prevents you from following:

?View Code ACTIONSCRIPT3
1
Error #2154: The NetStream Object is invalid. This may be due to a failed NetConnection.

Click here to open my example, or here to download the flex project.

You should see Your Broadcaster and your Receiver getting along just fine.