Using VideoDisplay with live streams
15 Feb 2006You can use VideoDisplay with live streams from Flash Communication/Media Server using following code.
- Create a directory called "flex_live_streaming" in C:\Program Files\Macromedia\Flash Media Server 2\applications\
- Place main.asc inside "flex_live_streaming" or "flex_live_streaming\scripts" directory. See detailed instructions.
<![CDATA[
import NetConnectionClient;
private var nc:NetConnection;
private var camera:Camera;
private var microphone:Microphone;
private var ns:NetStream;
private function initApp():void
{
camera = Camera.getCamera();
microphone = Microphone.getMicrophone();
}
private function publish():void
{
var streamName:String = "live_stream";
nc = new NetConnection();
//object where callbacks are defined.
nc.client = new NetConnectionClient();
nc.connect("rtmp://localhost/flex_live_streaming");
ns = new NetStream(nc);
ns.attachCamera(camera);
ns.attachAudio(microphone);
ns.publish(streamName);
//subscribe to live stream, by assigning the RTMP url to VideoDisplay.
videoDisplay.url = "rtmp://localhost/flex_live_streaming/" + streamName;
}
]]>
//NetConnectionClient.as
package
{
public class NetConnectionClient
{
public function NetConnectionClient()
{
}
public function onBWDone(... rest):void
{
}
public function onBWCheck(... rest):uint
{
//have to return something, so returning anything :)
return 0;
}
}
}
Download
If you notice the highlighted NetConnectionClient class in mxml code. Why do we need it?
We need it because main.asc invokes some functions(onBWCheck, onBWDone etc) on NetConnection.client while doing bandwidth-detection. If your NetConnection instance is missing these callback functions, you won't be able to publish/subscribe streams. It works fine with VideoDisplay because VideoDisplay seems to be having those functions defined.
If you are using Macromedia Flash Player's NetConnection and VideoDisplay together in your Flex application, you need to have those callback functions defined on NetConnection's instance, as shown above.
Note: This is workaround for current version(Beta 1) of VideoDisplay. Adobe Flex framework is still under development, so things might change later.
Hopefully we don't need any workaround. I wish, if there is a class in Flex framework which encapsulates NetConnection and all these callback so we can use it instead of using NetConnection.