Displaying a video in Flex using the NetConnection, NetStream, and Video classes
The following example shows how you can display a FLV file in a Flex application using the NetConnection, NetStream, and Video classes, as well as how to use the onMetaData and onCuePoint event handlers to handle video meta data and cue points.
<?xml version="1.0" encoding="utf-8"?> <!-- http://blog.flexexamples.com/2008/03/01/displaying-a-video-in-flex-using-the-netconnection-netstream-and-video-classes/ --> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" verticalAlign="middle" backgroundColor="white" creationComplete="init();"> <mx:Script> <![CDATA[ import mx.utils.ObjectUtil; private var nc:NetConnection; private var ns:NetStream; private var video:Video; private var meta:Object; private function init():void { var nsClient:Object = {}; nsClient.onMetaData = ns_onMetaData; nsClient.onCuePoint = ns_onCuePoint; nc = new NetConnection(); nc.connect(null); ns = new NetStream(nc); ns.play("http://www.helpexamples.com/flash/video/cuepoints.flv"); ns.client = nsClient; video = new Video(); video.attachNetStream(ns); uic.addChild(video); } private function ns_onMetaData(item:Object):void { trace("meta"); meta = item; // Resize Video object to same size as meta data. video.width = item.width; video.height = item.height; // Resize UIComponent to same size as Video object. uic.width = video.width; uic.height = video.height; panel.title = "framerate: " + item.framerate; panel.visible = true; trace(ObjectUtil.toString(item)); } private function ns_onCuePoint(item:Object):void { trace("cue"); } ]]> </mx:Script> <mx:Panel id="panel" visible="false"> <mx:UIComponent id="uic" /> <mx:ControlBar> <mx:Button label="Play/Pause" click="ns.togglePause();" /> <mx:Button label="Rewind" click="ns.seek(0); ns.pause();" /> </mx:ControlBar> </mx:Panel> </mx:Application>
View source is enabled in the following example.
Peter deHaan
Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.
-
Add Widgets (Content Sidebar)
This is your Content Sidebar. Edit this content that appears here in the widgets panel by adding or removing widgets in the Content Sidebar area.
33 Responses to Displaying a video in Flex using the NetConnection, NetStream, and Video classes
Leave a Reply Cancel reply
-
Categories
- Accordion
- AccordionHeader
- ActionScript
- AddChild
- AdvancedDataGrid
- Alert
- alpha
- Animate
- AnimateProperties
- Application
- Application (Spark)
- ArrayCollection
- BarChart
- baseColor
- beta
- beta1
- beta2
- Bitmap
- Bitmap/BitmapData
- BitmapData
- BitmapFill
- BitmapFill (Spark)
- BitmapGraphic
- BitmapImage
- BitmapImage (Spark)
- BitmapImageResizeMode
- Border (Spark)
- BorderContainer (Spark)
- Box
- BuildInfo
- Button
- Button (Spark)
- ButtonBar
- ButtonBar (Spark)
- ByteArray
- Camera
- Charting
- CheckBox
- CheckBox (Spark)
- ClassFactory
- CollectionEvent
- Color
- ColorPicker
- ColorUtil
- ComboBox
- ComboBoxArrowSkin
- Compiler
- Component
- Component (Spark)
- Configuration
- Container
- ContextMenu
- ContextMenuEvent
- ContextMenuItem
- CSSCondition
- CSSSelector
- CSSStyleDeclaration
- CurrencyFormatter
- CursorManager
- Data Binding
- DataGrid
- DataGrid (Spark)
- DataGridColumn
- Date
- DateBase
- DateChooser
- DateField
- DateFormatter
- Debugging
- DefaultComplexItemRenderer
- DefaultTileListEffect
- DropDownList
- DropDownList (Spark)
- DropDownListButtonSkin
- DropDownListSkin
- DropShadowFilter
- E4X
- Effects
- Ellipse
- EmailValidator
- Embed
- Event
- Fade
- FileFilter
- FileReference
- fill
- Filters
- Flash
- Flash Integration
- FlashVars
- Flex 3 SDK
- Flex Builder
- Flex Builder 3
- Flex SDK
- Flex4
- FLVPlayback
- FocusManager
- FontLookup
- Fonts
- Form
- Form (Spark)
- FormHeading (Spark)
- FormItem
- FormItem (Spark)
- Forms
- FTETextField (Spark)
- FullScreen
- FullScreenEvent
- FxAnimateColor
- FxButtonBar
- FxCheckBox
- FXG
- FxHScrollBar
- FxHSlider
- FxList
- FxNumericStepper
- FxRadioButton
- FxRotate3D
- FxScroller
- FxTextArea
- FxTextInput
- FxToggleButton
- FxVScrollBar
- FxVSlider
- getStyleDeclaration()
- GradientEntry
- Graphic (Spark)
- HBox
- HDividedBox
- HGroup (Spark)
- HorizontalLayout
- HorizontalList
- HSBColor (Spark)
- HScrollBar (Spark)
- HSlider
- HSlider (Spark)
- HTML template
- ID3Info
- Image
- Image (Spark)
- ImageSnapshot
- itemRenderer
- JointStyle
- Label
- Label (Spark)
- Legend
- LegendItem
- LigatureLevel
- Line
- LinearGradientStroke
- LineScaleMode
- LinkBar
- LinkButton
- List
- List (Spark)
- Menu
- MenuBar
- Metadata
- MetadataEvent
- Model
- Mouse
- MouseCursor
- MouseEvent
- Move
- Namespace
- NavigatorContent (Spark)
- needsSWF
- NetConnection
- NetStream
- Nightly Builds
- NumberBaseRoundType
- NumberFormatter
- NumberValidator
- NumericCompare
- NumericStepper
- NumericStepper (Spark)
- ObjectProxy
- ObjectUtil
- paddingLeft
- paddingRight
- Panel
- Panel (Spark)
- Parallel
- Path
- PieChart
- PieSeries
- PieSeriesItem
- PopUpAnchor (Spark)
- PopUpButton
- PopUpManager
- ProgrammaticSkin
- ProgressBar
- PropertyChangeEvent
- QName
- RadialGradient
- RadioButton
- RadioButton (Spark)
- RadioButtonGroup
- RadioButtonGroup (Spark)
- Rect
- RegExp
- Regular Expressions
- Repeater
- RichEditableText
- RichText
- RichText (Spark)
- RichTextEditor
- Rotate
- Rotate3D (Spark)
- Scroller (Spark)
- Sequence
- setStyle()
- SimpleText
- SimpleText (Spark)
- skinClass
- Slider
- SliderEvent
- SolidColor
- SolidColorStroke
- Sort
- SortField
- Sound
- SoundEffect
- Spinner (Spark)
- SpriteVisualElement (Spark)
- StageDisplayState
- States
- StringUtil
- StringValidator
- StyleManager
- Styles
- SWFLoader
- SWFObject
- System
- SystemManager
- TabBar
- TabBar (Spark)
- TabNavigator
- TabStopFormat
- Text
- Text Layout Framework (TLF)
- TextArea
- TextArea (Spark)
- TextBox
- TextConverter
- TextEvent
- TextFlow
- TextFlowUtil
- TextFormat
- TextGraphic
- TextInput
- TextInput (Spark)
- TextLayoutFormat
- TextView
- Themes
- TileLayout
- TileList
- TileOrientation
- Timer
- TitleWindow
- TitleWindow (Spark)
- TLF
- ToggleButton (Spark)
- ToggleButtonBar
- ToolTip
- Transition
- Tree
- TruncationOptions
- UIComponent
- UIFTETextField
- Updater
- URLLoader
- URLRequest
- URLUtil
- URLVariables
- ValidationResultEvent
- Validator
- Validators
- VBox
- VDividedBox
- Vector
- VerticalLayout
- VerticalLayout (Spark)
- VGroup (Spark)
- Video
- VideoDisplay
- VideoElement
- VideoElement (Spark)
- VideoEvent
- VideoPlayer (Spark)
- VideoPlayerScrubBar
- ViewStack
- VScrollBar (Spark)
- VSlider
- VSlider (Spark)
- XML
- XMLList
- XMLListCollection
- ZipCodeValidator
- ZipCodeValidatorDomainType
- Zoom
-
Articles
- December 2010
- November 2010
- October 2010
- September 2010
- August 2010
- July 2010
- June 2010
- May 2010
- April 2010
- March 2010
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
-
Meta


This is a very good and simple example! I really do find your blog incredibly useful.
I’m pretty new to flex and am checking out the video features. I’m seeing everywhere examples (yours being the simplest) on streaming .flv files but hardly any on doing the same for H.264 files. I tried it in this example with its location on my fms and it did not work. Is there anything else I need to add?
Also, I was wondering if there was any specific reason you put the video in UIComponent instead of just a videoDisplay component?
Thanks!
Hey, Nice blog. Slamice is right. Even I wanted to know whether it is possible to play H.264 contents on flex.
Shiv: VideoDisplay extends UIComponent, and contains an instance of Video object so you wouldn’t place a Video object inside a VideoDisplay because it would be redundant. The reason it’s added to a UIComponent is because UIComponent is the very top component of Flex. All Flex containers (i.e. Canvas, Panel, HBox, etc) require their children to be a UIComponent as well. Video does not extend from UIComponent, so by itself, it’s not a Flex component. It’s originally a flash component so you have to add it to a UIComponent before adding it to any Flex Component.
If you are doing simple playback of video VideoDisplay a really easy way to playback simple video. If you are doing something more exotic then Video, NetConnection, NetStream, etc are going to give you more control but they are much harder to use.
Can we do the same thing with a XML playlist ? if yes how do we do it ?
Hello, I do not have a lot of experience with Flex and am completely new at using video in any way. I am basing a Flex custom omponent on the above code, but am working with a video that is 23x the size of the sample here.
I am thinking of some kind of progress bar before the buttons, screen, etc. are visible. I would appreciate any ideas on how to accomplish this.
Best regards,
Carlos
Hi,
Just wondering if I can play two netstreams in one netconnection simultaneously.
When I tried that, both videos can’t play well. Please advise.
Thanks.
Hey, Nice blog. Slamice is right. Even I wanted to know whether it is possible to play H.264 contents on flex.
http://www.oyunet.net
Please provide a html java script version as well of this example if possible thank you.
Is it me or this code should not work:
in init() the following call is made:
nsClient.onMetaData = ns_onMetaData;
whereas as method ns_onMetaData should take an input Object:
private function ns_onMetaData(item:Object):void {…}
And, furthermore, ns_onMetaData() returns void, but in init() it is supposed to assign a value to nsClient.onMetaData.
Am I not understanding basic AS syntax or what?
Thanks a lot
Hi Everyone,
Can any one help me to convert any type of Video files into .flv format (i.e) flash video format.
I mean, if we browse and select any type of video, it (Flex Program) should convert it to .flv format.
Thanks in Advance……
The example was very helpful.
I have found that the NetStream really shouldn’t be opened until you are sure you have a good NetConnection.
_nc.addEventListener(NetStatusEvent.NET_STATUS, onConnectStatusEvent, false);
Also, to the previous poster… vlc works very nicely.
http://wiki.videolan.org/Documentation:Streaming_HowTo/Advanced_Streaming_Using_the_Command_Line
Hi,
Ur example was really useful.
Thanx,
Best regards,
Jobin
Hi,
Very nice blog. I have a problem i want a live video streaming but i m unable to do this. While my code is working properly on local system but when i have upoaded on server it is not creating any file. I think u will solve my problem. I have mentoned my fms path:
quick question, can i send a camera using netstream?
Thanks for this.
I’ve been moving a lot of my actionscript 2.0 mush into nice, clean actionscript 3.0 and this has helped alot.
Keep on coding! :)
Hi:
The example was very helpful. I am learning Flex/AS3 and have created a simple Player to play H264 video. I am using var videoFrame:VideoDisplay; in the player. When I set the videoFrame.source property to something like http://myserver.com/test.mp4 all is well. When I try to use a URL like http://myserver.com/getvideo.php?id=12345 (which returns a valid mp4 file with correct http headers etc.) I do not see the video play. I currently use the QT plugin in a web page with URLs like the one above and it works fine.
If I use an approach like your example with the NetConnection and NetStream objects should the flash player be able to load and play video content that comes from a server script instead of directly from a video file in the servers document root?
Thanks for any advice.
Mark
PS – My SWF file and all the video content and PHP script are in the same location on the server so my assumption is that this not a security issue.
When I try to replace the video source http…. for an rtmpe/rtmp it doesn’t work. Why?
Thanks Peter for a nice and simple example,
i was just wondering if flex can handle any other streams which are not in .flv formats like the audio streaming links , or streaming audio and video content from a radio station streams
much like the .asx, .asf files .
may be the Flex Media Server and Encoder can help me stream such content but is there any other way to stream such content.
i even checked with OSMF it doesnt seem to support it as well.
i’ll appreciate your help
Thanks,
Lisa
So, videodisplay is depricated in flex 4. How do you do this with videoplayer?
i want information about flex
This is a a very simply way to done it. this way is so nice
Error loading the demo:
Severity and Description Path Resource Location Creation Time Id
unable to open ‘C:….\Flex Builder 3\NetConnection_NetStream_Video_test\libs’
NetConnection_NetStream_Video_test Unknown 1267650308296 39
Do I need Flash CS4 for this or is there a .swc I need to download somewhere?
Thanks in advance!
@GJHDigital,
No, I just copy/pasted the code into an existing project and it worked fine. If you imported the code via the ZIP file, you may have to double check that your project has a /libs/ folder. I think a few of these earlier ZIP files had a problem where the /libs/ folder wasn’t exporting since it was empty.
Peter
how to coonect video files into a flex
hey,
it’s allright when do we want to use VideoDisplay class instead,
see this post here!
I have created a flex application. When i play a file in my application it doesnt support full screen and on top of that the you-tube player is separated from flex video player. So both work as seperate players. What do you suggest me to do???
Hi,
I have a trouble with a h264 video. The video is played only when it’s fully loaded in my browser.
I tried with your code and also with mx:VideoDisplay. Actually the metadata are received only at the download is complete.
I also try with the html5 video tag. With the video tag, the first frame appeared subitely when it’s available.
Is it a known issue of the avc1 codec? I didn’t find much information on the net.
It is common problem of mp4 container for flash video. It happens because MetaData is not at the begining of video file. You should move it using some util. Google for MOOV f4v;
Thank Verindra
My Stream Live From RED5 to FLEX Client Problem [Solved] from your example. heres what I did. For other who have this problem , just take out what you dont need from my Code
<!–
–>
Hi ,
Can anyone help on this……
there is player for that i’m displaying a voting widget.if the player to be maximised widget not displaying.i need to display thatone.
can anyone plzzzz
I want the code for streaming live audio radio from internet in my flex blackberry application
Thank you a lot! With some modifications, this really helped me develop the final project of my graduate studies.
Greetings from Londrina/Brazil!