Skip to main content
February 20, 2010
Answered

Can't Stream File using Video object

  • February 20, 2010
  • 1 reply
  • 720 views

I'm trying to get my video files to stream from my local version of flash media server to my instance of a flex application. I'm able to connect to the server and I'm receiving the NetConnection.Connect.Success code from the server. When I debug the application, I'm finding that I'm getting NetStatusEvent objects with the codes: NetStream.Play.Reset and NetStream.Play.Start. And the info object's details property is "sample.flv" which is the video file that I have sitting in my "FMSTesting" applicaiton folder. So my video is in my application folder at: "rtmp://localhost/FMSTesting/sample.flv", and my Flex application outputs to the webroot of the flash media server "http://localhost/FMSTesting/bin-debug/FMSTesting.html"

So I'm not sure if my setup is wrong or not, but I think it's ok because I can connect to the server just fine. I just can't get my stream to play on the Video object. Here's my code, please help if you can. I also attached the code

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init();">
<mx:Script>
<![CDATA[
import mx.core.UIComponent;
import mx.rpc.events.ResultEvent;
import flash.net.NetConnection;
import flash.net.navigateToURL;
import flash.events.NetStatusEvent;
import flash.events.StatusEvent;
import flash.text.TextField;
import flash.net.NetStream;
import flash.media.Video;

public var wrap:UIComponent;
public var nc:NetConnection;
public var ns:NetStream;
public var myVid:Video;
public function init():void{
nc = new NetConnection();
nc.addEventListener(NetStatusEvent.NET_STATUS,onConnect);
nc.connect("rtmp://localhost/FMSTesting","bonnetbe");
wrap = new UIComponent();
wrap.x = 10;
wrap.y = 10;
wrap.width =210;
wrap.height = 200;

}

//public function onSayHello(

public function onConnect(event:NetStatusEvent):void{

myText.text = "The connection is "+nc.connected + event.info.code;



switch (event.info.code)
{
case "NetConnection.Connect.Success":
myText.text += ("Congratulations! you're connected" + "\n");
makeVideos();
break;
case "NetConnection.Connect.Rejected":
myText.text += ("Oops! the connection was rejected" + "\n");
break;
case "NetConnection.Connect.Closed":
myText.text += ("Thanks! the connection has been closed" + "\n");
break;
}


}
public function makeVideos():void{
ns = new NetStream(nc);
ns.addEventListener(NetStatusEvent.NET_STATUS,netStatusHandler);
ns.client = this;
ns.play("sample.flv");

myVid = new Video(200,180);
myVid.attachNetStream(ns);
myVid.opaqueBackground = true;
myVid.smoothing = true;
myVid.width = 200;
myVid.height = 180;
wrap.addChild(myVid);





myCanvas.addChild(wrap);

}
public function callClient(event:Event):void{
nc.call("magic",new Responder(myResponder,statusResponder), "World");
nc.call("moreFunc",new Responder(myResponder2,statusResponder2),"benjamin");
//myText.text+= "still connected: "+nc.connected;
}
public function netStatusHandler(event:NetStatusEvent):void{
trace(event);
trace(myVid.videoHeight+" : "+myVid.videoWidth);
}
public function myResponder(result:Object):void{
myText.text += "the result: "+result;
}
public function statusResponder(status:Object):void{
myText.text += "some status: "+status.code;
}

public function myResponder2(result:Object):void{
myText.text += "result2: "+result;
}

public function statusResponder2(status:Object):void{
myText.text += "status 2: "+status.code;
}

public function updateFunc(event:Event):void{
changeText.text = "testing: "+nc.connected;
nc.close();
}

]]>

</mx:Script>
<mx:TextArea id="myText" text="Hello Ben" width="271" height="155" x="311" y="138"/>
<mx:Text x="10" y="192" text="Text" width="144" height="67" id="changeText"/>
<mx:Button x="27" y="378" label="Button" id="funcCall" click="updateFunc(event);"/>
<mx:Button x="135" y="378" label="callFunc" id="callFunc" click="callClient(event);"/>
<mx:Canvas x="311" y="10" width="200" height="120" id="myCanvas"/>
<mx:VideoDisplay x="759" y="62" width="300" height="250" id="vidObj"/>

</mx:Application>

    This topic has been closed for replies.
    Correct answer Janaki Lakshmikanthan

    Hi,

    Your code is perfectly fine except for one statement.

    ns.play(

    "sample.flv");

    Since you want to play vod, give the stream name without extension like -

    ns.play(

    "sample");

    But why you got NetStream.Play.Reset and NetStream.Play.Start events? It is because you have not mentioned the 'start' and 'length' values in your ns.play() method. Which takes the default for 'start' as -2, which means that Flash Player first tries to play the live stream specified in stream_name. If a live stream of that name is not found, Flash Player plays the recorded stream specified in stream_name. If neither a live nor a recorded stream is found, Flash Player opens a live stream named stream_name, even though no one is publishing on it. When someone does begin publishing on that stream, Flash Player begins playing it.  Since you have mentioned the stream name with extension, FMS considers it as a stream which is not found in the server and started waiting for a live stream with the name 'sample.flv'.

    Best practice is always give the 'start' and 'length' of the stream when calling NetStream.play() method.

    Regards,

    Janaki L

    1 reply

    Janaki Lakshmikanthan
    Adobe Employee
    Janaki LakshmikanthanCorrect answer
    Adobe Employee
    February 22, 2010

    Hi,

    Your code is perfectly fine except for one statement.

    ns.play(

    "sample.flv");

    Since you want to play vod, give the stream name without extension like -

    ns.play(

    "sample");

    But why you got NetStream.Play.Reset and NetStream.Play.Start events? It is because you have not mentioned the 'start' and 'length' values in your ns.play() method. Which takes the default for 'start' as -2, which means that Flash Player first tries to play the live stream specified in stream_name. If a live stream of that name is not found, Flash Player plays the recorded stream specified in stream_name. If neither a live nor a recorded stream is found, Flash Player opens a live stream named stream_name, even though no one is publishing on it. When someone does begin publishing on that stream, Flash Player begins playing it.  Since you have mentioned the stream name with extension, FMS considers it as a stream which is not found in the server and started waiting for a live stream with the name 'sample.flv'.

    Best practice is always give the 'start' and 'length' of the stream when calling NetStream.play() method.

    Regards,

    Janaki L

    February 22, 2010

    Thanks Janaki, your answer really helps and I have it working now. I really appreciate it.