as3不能播放flv
flex下建立一个ActionScript项目,名字NewHello。本意要播放flv文件,可没有任何影像出现。项目大致目录结构如下
NewHello
---src
---(默认包)
---NewHello.as
---connectThis(包)
---Connect.as
第一做法:
NewHello.as
package{ import connectThis.Connect; import flash.display.MovieClip; import flash.display.Sprite; import flash.media.Video; import flash.net.NetConnection; import flash.net.NetStream; public class NewHello extends Sprite { private var con:Connect; public function NewHello() { con = new Connect(); } }}
package connectThis{ import flash.display.DisplayObjectContainer; import flash.display.MovieClip; import flash.display.Sprite; import flash.display.DisplayObjectContainer; import flash.media.Video; import flash.net.NetConnection; import flash.net.NetStream; public class Connect extends Sprite//DisplayObjectContainer//MovieClip// { public function Connect() { //创建NetConnection对象 var myNc:NetConnection=new NetConnection(); myNc.connect(null); //使用onMetaData函数,Flash用来寻找这个方法 var metaObject:Object=new Object(); metaObject.onMetaData=onMetaData; //构造NetStream对象并且连接创建的myNc对象 var myNs:NetStream=new NetStream(myNc); myNs.play("http://192.168.1.126/sample.flv"); //发送所有onMetaData调用到NetStream上 myNs.client=metaObject; //创建video尺寸 var video:Video=new Video(160,120); //加载Video到NetStream上去,添加到display列表 video.attachNetStream(myNs); addChild(video); } private function onMetaData(data:Object):void { //发送Flash需要的Metadata数据 } }}
package{ import flash.display.MovieClip; import flash.display.Sprite; import flash.media.Video; import flash.net.NetConnection; import flash.net.NetStream; public class NewHello extends Sprite { public function NewHello() { var myNc:NetConnection=new NetConnection(); myNc.connect(null); //使用onMetaData函数,Flash用来寻找这个方法 var metaObject:Object=new Object(); metaObject.onMetaData=onMetaData; //构造NetStream对象并且连接创建的myNc对象 var myNs:NetStream=new NetStream(myNc); myNs.play("http://192.168.1.126/sample.flv"); //发送所有onMetaData调用到NetStream上 myNs.client=metaObject; //创建video尺寸 var video:Video=new Video(160,120); //加载Video到NetStream上去,添加到display列表 video.attachNetStream(myNs); addChild(video); } private function onMetaData(data:Object):void { //发送Flash需要的Metadata数据 } }}
package{ import connectThis.Connect; import flash.display.MovieClip; import flash.display.Sprite; import flash.media.Video; import flash.net.NetConnection; import flash.net.NetStream; public class NewHello extends Sprite { private var con:Connect; public function NewHello() { con = new Connect(); [color=#FF0000]addChild(con);[/color] } }}
[解决办法]
con = new Connect();
addChild(con);
实例化组建后要追加到舞台上