RSS

Connecting to a Flash Media Server using RTMP

This entry was posted on Aug 03 2008

RTMP is the protocol used by Adobe’s Flash Media Server to stream content into flash. Most of the help documentation doesn’t touch much on this method of connection and is limited to sources not easily available to users.

This article should be used by those with an understanding of video playback using actionscript 3. If that isn’t you, please read this article before continuing.

1.) In order to use the RTMP protocol, you will need to install and run a version of Adobe Flash Media Server. Adobe has released a free developer version the limits you to 10 connections. That will be MORE than enough to do some basic development. Please make sure you are running or have access to a Flash Media Server before continuing to the next step.

1.) (B) If you are having trouble installing or setting up an app, please read this article.

2.) Now that you have access to a Flash Media Server, we will start with some basic code that should look familiar if you have worked with actionscript and video before. For this step, you will need to know the URI for the server. If you installed Flash Media server on the machine you are running on, you can use “localhost” for the URI. We will be using the “vod” app that comes pre-installed on the server for this example.

var nc:NetConnection = new NetConnection();
nc.connect("rtmp://YOUR_SERVER_URI/vod/");

If we were loading an flv file without streaming, you would have passed null to the connect() method instead.

3.) The only thing left is to load the stream, instead of loading an FLV video from a URL. This is actually much easier than you think. Instead of passing a URLRequest to the NetStream.play() method, you would instead pass the name of the stream you wish to play from within your app (this is the name of the FLV file on the server, but without the .FLV extension). To playback an HD streaming file, you will need to format your stream name slightly different: “mp4:NAME_OF_STREAM.mp4″

  • Please note that you can not setup your NetStream until after the NetConnection has successfully connected. You will know this from the NetStatus event: NetConnection.Connect.Success that is dispatched.
var ns:NetStream = new NetStream(nc);
ns.play("NAME_OF_STREAM");
// ns.play("mp4:NAME_OF_STREAM.mp4");  // this will stream an HD movie instead.

4.) That’s it! I hope this article has helped. Please comment below with any questions and I would be glad to help answer them.

For a more informative tutorial, check out this one from Peachpit.


42 Responses to “Connecting to a Flash Media Server using RTMP”

  1. Hello Andrew,
    I use flvplayback (CS3) to acces: rtmp://f-streaming.zdf.de/live/dic-hd.
    It doesn’t work.
    Can you help me
    thanks


  2. Here, my code:

    var nc:NetConnection = new NetConnection();
    nc.connect(“rtmp://f-streaming.zdf.de/live/”);
    var ns:NetStream = new NetStream(nc);
    ns.play(“dic-hd”);

    thanks


  3. Robert,
    Your problem is you have to wait until the NetConnection has successfully connected before you can create your NetStream (this is noted above under #3).


  4. thanks so much! it start working….
    Robert.


  5. Andrew,
    Can I know number of bytes (to calculate bandwidth) for stream RTMP?
    thanks
    Robert.


  6. thank you very much for this very useful post…it help me a lot…


  7. Robert,
    Using Flash Media Server 3 you can have the server do the bandwidth testing for you and send back an estimated user bandwidth.

    Use this link for more information on detecting your bandwidth:
    http://livedocs.adobe.com/flashmediaserver/3.0/hpdocs/00000070.html


  8. Andrew,

    I see but i like to know the real bandwidth of stream RTMP. I need a parameter same as “bytesLoaded” in Progressive mode.
    Do you have any idea?

    thanks.


  9. If it is true streaming, you shouldn’t need to check the bytesLoaded – you should be checking to see if the buffer is full instead, using NetStream.bufferTime and NetStream.bufferLength…


  10. using NetStream.bufferTime and NetStream.bufferLength, we can estimate the relative bandwidth (%) but i would like the absolute bandwidth (par ex: 1000kb/s).


  11. Robert,
    If you are using Flash 10, you have access to the NetStream.info (NetStreamInfo class) object that has plenty of the information about the video and audio bandwidth.
    http://help.adobe.com/en_US/AS3LCR/Flash_10.0/flash/net/NetStreamInfo.html

    In Flash Player 9 and higher you can access the data rate as part of the Meta Data that is accessible from the onMetaData event that is fired by the NetStream.client. You can find out more about the NetStream.client object here:
    http://www.adobe.com/devnet/flash/quickstart/metadata_cue_points/

    Hope this helps :)


  12. thanks.
    I will try…
    Robert.


  13. Andrew,
    I’d like capture an image of rtmp.
    But i have no idea. Have you any idea?
    thanks
    Robert


  14. Robert,
    Are you just wanted to capture a still frame of media you are streaming over RTMP? If not, RTMP is a streaming protocol for video and audio. Since an image doesn’t have media to stream, pulling it over RTMP isn’t necessary. Instead, you just need to load the video in using HTTP (standard way to load data over the internet) and an instance of a Loader object. I’ve got a tutorial on how to load external images here:
    http://blog.728media.com/2009/03/11/how-to-load-external-images-in-actionscript-30/

    If you are trying to capture a screenshot of a still frame of video you are streaming, you will need to draw the BitmapData of your Video object. Try this tutorial here:
    http://darylteo.com/blog/2006/12/17/bitmapdatadraw/


  15. Andrew,
    I read a stream rtmp. I’d like capture a frame of video.
    Thanks
    Robert


  16. Hi ,

    var my_nc:NetConnection = new NetConnection();
    my_nc.connect(“rtmp://localhost/appName/appInstance”);

    var my_ns:NetStream = new NetStream(my_nc);
    my_video.attachVideo(my_ns); //my_video is the video instance.

    my_ns.play(“stephen”);

    Its work fine when i try to connect my local media server but when i try to connect to the client side Media server by specifying its path then it does not work…..

    can u plz help me out..

    Thanks in Advance


  17. Amit,
    Your problem is you have to wait until the NetConnection has successfully connected before you can create your NetStream (this is noted above under #3).


  18. Hello Amit,

    if possible, there are some Security error. please check the Cross domain policy file & do changes as per need.


  19. Hello Andrew & Shaival,

    if i trace and i got the trace message Net Connection is established. Now i wait for a min for Net Streaming although i cant find any stream. so, what have to do now ? is there any problem with security ? or any other issues ?

    I have already changed Cross domain & i have put * in allowed domain tag.

    Thanks in Advance.


  20. Amit,
    If you are unable to find the stream (and are not getting an error when trying to connect), most likely the stream you want doesn’t exist or is unavailable.

    It might still be a security error, please read this:
    http://www.adobe.com/devnet/flashplayer/articles/flash_player_9_security.pdf


  21. After reading your post, I was excited. Unfortunately I can not get the flv to play. My videp player and buffer appear but, nothing plays.

    //———-CREATE NET CONNECTION AND NET STREAM OBJECTS———-
    nc = new NetConnection();
    //nc.connect(null);
    nc.connect(“rtmp://……./…/…./../home.flv”);
    ns = new NetStream(nc);
    ns.setBufferTime(bufferSize);
    ns.play(“home”);
    //———-ATTACH VIDEO STREAM TO videoObject———-
    this.videoObject.attachVideo(ns);

    Any initial thoughts?


  22. Marc,
    nc.connect() should be passed the URI of the location of your app, without the filename appended. When you call ns.play(), that is when you can pass in the video file name (without the .flv extension).

    Are you working in AS3? If so, you will need to make sure the NetConnection is connected before creating your NetStream object. (see previous comment replies).


  23. Andrew,
    thanks for taking the time to respond. And thanks for that bit of info. I actually discovered that my server path needed to be relative, not absolute. Which seems to vary from application to application. Crazy. I’m still sorting it out compared to previous players that I’ve done.

    Great blog and thanks for sharing.
    Looking forward to reading more.


  24. Hi, i think i have the idea of how this is working, but at first glance, isee many saying you need to point to your app, rtmp://localhost/app/

    what is the app, i have a player in public_html/player/player.swf, and i have the videos both flv and mp4, in public_html/videos and some outside public in pvideos/

    I see how doing the flv:video.flv and mp4:video.mp4 works, im not sure where the app fits in, is this just basically pointing the streaming application?


  25. GreenScripts,
    When you are working with server software on the same machine the app lives on, you use localhost instead of the server’s IP or domain name. App just refers to the interaction that is calling to the server. This could be Flash, PHP, Ruby, etc…


  26. Hello,

    I have a link to a video which for some reason doesn’t let me ‘connect’ the server. It says connection rejected. However if I put the same URL in the FLVPlayback component CS4, it starts to play it.

    Is there a way to play a RTMP video without connecting to the server first?


  27. Muhammad,
    The FLVPlayback component still connects to the server in order to playback an FLV file. Files delivered over RTMP must connect to the server before they are able to connect to the file.

    Remember, to connect to the server, you must only input the URL to the FMS app on the server when you connect the NetConnection. Leave out the last part of your URL that points to the FLV file. The FLVPlayback component strips that for you automatically, that is probably why it works.

    Example:
    URL to the FLV File:
    rtmp://media.myserver.com/myApp/video.flv

    The URL you would pass in to the NetConnection would be this:
    rtmp://media.myserver.com/myApp


  28. How to return the NetStream.BytesLoaded on the rtmp?


  29. clefoo,

    Since you are streaming media the NetStream.bytesLoaded variable is unnecessary. Instead you should be watching the NetStream.bufferTime and NetStream.bufferLength variables to supervise the stream.


  30. Hello, I have read very carfully your #3, that you are referring people to, but can’t say I understnd, and sorry for the positng. So you say: “Please note that you can not setup your NetStream until after the NetConnection has successfully connected”, but how to code this? nc.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus); is giving me Access of undefined propery… Thank you in advance for your assisatance.


  31. This one is really great but my probs is here
    i got all the information but still i am not able to run my FMS3 and xampp togather. Is there any solution for it, forgot to mention i am running both in same computer.
    Thanks in advance….


  32. hi,
    i want to hide the path of my video on the server, so i am trying to pass a handler in the netstream.play() function, but it gives an error, is there a way to call the video by passing the handler


  33. Joshua,
    You must pass in the name (or URL) of the stream that you want to play. You could try passing in a webservice URL as the parameter that returns the name of the stream you wish to play. I’m not sure if this would still hide the real URL in the HTTP requests, but it would be worth a shot.


  34. Hi there, I agree with previous comments that it was not clear from the examples provided here how to check that a successful connection was established (named here point #3).

    Have a look at the following link for the code explaining how to do so:

    http://livedocs.adobe.com/livecycle/es/sdkHelp/common/langref/flash/net/NetConnection.html

    I hope this helps.


  35. Hi, anyone still out there?

    Has anyone figured out how to implement this code to check the connection? I’ve been searching for 2 days and this is the only conversation I’ve found. If anyone can post the code, or point me to a tutorial that shows you how to finish the code to play an rtmp stream from a 3rd party server, I (and it seems a bunch of other people) would be most appreciative.

    Thanks!


  36. Hello amit doshi this will defiantly help you.

    var nc:NetConnection;
    var ns:NetStream;
    var _video:Video = new Video(320,240);
    var _dur:Number = new Number();
    var _cur:String = new String();
    addChild(_video);
    //_video.x = 200;
    connect();
    function connect()
    {
    NetConnection.defaultObjectEncoding = flash.net.ObjectEncoding.AMF0;
    nc = new NetConnection();
    nc.client = this;

    //ns.client = {onMetaData:ns_onMetaData, onCuePoint:ns_onCuePoint};
    nc.objectEncoding = flash.net.ObjectEncoding.AMF0;
    nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
    nc.connect(“rtmp://localhost/oflaDemo/”);
    }
    function netStatusHandler(event:NetStatusEvent)
    {
    if(nc.connected)
    {
    ns = new NetStream(nc);
    ns.client = {onMetaData:ns_onMetaData, onCuePoint:ns_onCuePoint};
    _video.attachNetStream(ns);
    _video.attachNetStream(ns);
    //ns.play(“mp3:03 Dupatta_Tera_Nau_Rang_Da.mp3″);
    ns.play(“9.flv”);
    }
    }
    function ns_onMetaData(item:Object):void {
    //trace(“metaData”);
    // Resize video instance.
    _video.width = item.width;
    _video.height = item.height;
    // Center video instance on Stage.
    _video.x = (stage.stageWidth – _video.width) / 2;
    _video.y = (stage.stageHeight – _video.height) / 2;
    }

    function ns_onCuePoint(item:Object):void {
    trace(“cuePoint”);
    trace(item.name + “\t” + item.time);
    }


  37. i always watch streaming videos online, they are sort of my past time.;;;


  38. Hello,

    Why does the FLV Playback component does play MOV and MP4 files, while our flash.media.Video class does not play these files. It just does nothing. Sometimes the audio plays, but video does not work for any of these?

    Can you please help me understand this?

    Thanks!


  39. Think of the Video class as the display object or the video. It doesn’t actually handle the loading and playback of the video. That Is handled by the NetStream object. You then attach the NetStream to the Video object. Also make sure the MOV or MP4 is encoded using the h.264 codec.


  40. Hi
    How do I connect a outside flash media server?


  41. Adem,
    If by outside you mean not on your local machine (localhost) then you would input the domain or IP of your media server in place of “YOUR_SERVER_URI” in my code example above. Whoever is your server admin can get you that.


  42. What is YOUR_SERVER_URI?
    this?

    YOUR_SERVER_URI=localhost

    or

    YOUR_SERVER_URI=127.0.0.1


Post a Comment