[AS3]FLVStream类-检测flv文件类型的工具类
本类是利用flv文件头特征信息来判断文件的真实类型是否为flv文件,不是原来简单地通过文件扩展名来判断了。
MediaEvent类-FLVStream用到的事件类
-
-
-
-
-
-
- package com.klstudio.media {
-
- import flash.events.Event;
-
- public class MediaEvent extends Event{
-
- public static const FLV:String = "flv";
- public static const ERROR:String = "error";
-
- public function MediaEvent(type:String,bubbles:Boolean=false,cancelable:Boolean=false){
- super(type, bubbles, cancelable);
- }
-
- }
-
- }
FLVStream类
-
-
-
-
-
-
- package com.klstudio.media {
-
- import flash.net.URLStream;
- import flash.net.URLRequest;
- import flash.events.*;
- import flash.utils.ByteArray;
-
- import com.klstudio.util.StringUtil;
-
- [Event(MediaEvent.FLV)]
-
- public class FLVStream extends URLStream {
- private var _hasClose:Boolean;
- private var _bytes:ByteArray;
- private var _flv:Boolean;
- private var _url:String;
- public function FLVStream(url:String,close:Boolean=true):void {
- _url = url;
- _flv = false;
- _bytes = new ByteArray();
- _hasClose = close;
- addEventListener(ProgressEvent.PROGRESS, progressHandler);
- }
-
- public function read():Boolean{
- if(_url == null || StringUtil.trim(_url).length== 0){
- return false;
- }else{
- try{
- var request:URLRequest = new URLRequest(_url);
- load(request);
- }catch(error:Error){
- return false;
- }
- }
- return true;
- }
-
- private function progressHandler(event:ProgressEvent):void{
- if(bytesAvailable >= 3){
- readBytes(_bytes,0,3);
- _flv = _bytes.readUTFBytes(3).toLocaleUpperCase() == "FLV";
- dispatchEvent(new MediaEvent(MediaEvent.FLV));
- if(_hasClose){
- close();
- }
- removeEventListener(ProgressEvent.PROGRESS, progressHandler);
- }
- }
-
-
- public function isFlvFile():Boolean{
- return _flv;
- }
-
- }
-
- }
FLVStream调用实例
- package project.test {
- import flash.display.*;
- import com.klstudio.media.*;
- public class Test10 extends Sprite{
- private var _fs:FLVStream;
- public function Test10(){
- _fs = new FLVStream("http://video-v1.9you.com/421b47ffd946ca083b65cd668c6b17e6/flv/o/v/ovo5b516b.flv");
- _fs.addEventListener(MediaEvent.FLV,flvHandler);
- _fs.read();
- }
-
- private function flvHandler(event:MediaEvent):void{
- trace(_fs.isFlvFile());
- }
- }
- }
当然同样的原理,也可以写一个用来检测swf等其他文件类型了。只要知道文件头的特征信息就可以了。