问题
I am trying to publish the video of a webcam to a Flash Media Server 2. My code is working in the Flash standalone player (tested with 10.0 and 10.2), but not in the browser plugin (tested with 10.2, both in IE and Opera. The connection to my FMS is working successfully, but after the publish, nothing happens, I never get the NetStream.Publish.Start Event. On the server I can see the connection in the management console, even the stream in the streams tab. But I cannot connect to that strea.
Anybody an idea what could be going wrong?
NetConnection.defaultObjectEncoding = ObjectEncoding.AMF0; // needed to get correct connection to FMS2
private function netStatusHandler(event:NetStatusEvent):void {
output.appendText("\n" + event.info.code);
switch (event.info.code) {
case "NetConnection.Connect.Success":
output.text = ("Connection successful, streaming camera...");
connectCamera();
break;
case "NetConnection.Connect.Failed":
break;
case "NetStream.Play.StreamNotFound":
break;
case "NetStream.Publish.Start":
output.appendText("\nPublishing video!");
break;
}
}
private function connectCamera(ev:Event = null):void {
var stream:NetStream = new NetStream(connection);
stream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
stream.attachCamera(camera);
videoURL = createGUID();
stream.publish(videoURL, "live");
output.text = "publish stream...";
}
回答1:
Ok, I found what my problem is here: I declare the reference to the stream variable inside the connectCamera function:
private function connectCamera(ev:Event = null):void {
var stream:NetStream = new NetStream(connection);
}
So stream is only declared inside the scope of that function. This doesn't seem to be a problem in the standalone player, but it is in the browser plugin. The browser plugin seems to do a much more thourough job on the garbage collector, garbage collecting my stream after the function has executed. So what I have to do is declare the stream variable outside of the function scope, inside the class scope.
var stream:NetStream;
private function connectCamera(ev:Event = null):void {
stream = new NetStream(connection);
}
You should always declare stuff that you need later as a field on your class, and not inside a function. You just never know when GC might clean up that stuff.
来源:https://stackoverflow.com/questions/5423002/netstream-publish-webcam-to-fms-working-in-standalone-player-but-not-in-browser