Using VideoDisplay with live streams

You 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.
<mx:Application xmlns:mx="" xmlns="*" creationComplete="initApp()">
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();
ns = new NetStream(nc);
//subscribe to live stream, by assigning the RTMP url to VideoDisplay.
videoDisplay.url = "rtmp://localhost/flex_live_streaming/" + streamName;
<mx:Button label="Publish" click="publish()"/>
<mx:Panel title="Live Stream">
<mx:VideoDisplay id="videoDisplay" width="180" height="120" live="true" autoPlay="true"/>


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;

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.

  • Thanks Abdul for this tip. your blog is great place to find about new things 🙂

  • I can’t for the life of me get this app to work. I never get a stream or a popup asking to use the camera… I’ve change the namespace to 2006 and the videoDisplay.url to videoDisplay.source but to no avail. Would you consider updating the code for beta 3? Thanks!!! (You rock either way)

  • Bambang

    How to connect a CCTV’s camera to VideoDisplay using Flex 2? Thanks.

  • @Bambang: You might want to see if Red5 or Flash-Media-Server can let you stream content from CCTV? Does your CCTV come with camera driver? If it shows up in flash player’s setting (camera) section, that means you can stream the content…
    You can use Flash Media Encoder (now available on adobe site or to test it out

  • abhishek

    when i try this code , it not works , not showing anything,
    i want to show to all on server watever my camera is capturing from my pc ,
    could u tell how can it be done ?