Flash中先获取flv的尺寸然后再显示的方法
开发中需要用到将视频用原始大小显示出来。可用NetStream类来加载视频,获得其元数据(metadata),然后获得视频的尺寸。下面这段代码演示获得flv视频文件的尺寸,对于非flv视频文件,则默认为800×640的尺寸,然后将尺寸传给回调函数:
public static function getVideoSize(url:String, callback:Function):void
{
if(url && url.indexOf(".flv") > 0)
{
var n:NetConnection = new NetConnection();
n.connect(null);
var ns:NetStream = new NetStream(n);
ns.client = {};
ns.client.onMetaData = function (item:Object):void {
var width:Number = item.width;
var height:Number = item.height
ns.close();
if(callback != null)
{
callback(width, height);
}
};
ns.play(url);
}
else
{
if(callback != null)
{
callback(800, 640);
}
}
}
{
if(url && url.indexOf(".flv") > 0)
{
var n:NetConnection = new NetConnection();
n.connect(null);
var ns:NetStream = new NetStream(n);
ns.client = {};
ns.client.onMetaData = function (item:Object):void {
var width:Number = item.width;
var height:Number = item.height
ns.close();
if(callback != null)
{
callback(width, height);
}
};
ns.play(url);
}
else
{
if(callback != null)
{
callback(800, 640);
}
}
}
版权所有,欢迎转载