使用FileReferenceList类加载多个flv视频

时间:2011-01-08 10:00:39

标签: flex actionscript-3 flex4 flash-builder

以下是我使用FileReference类加载一个视频的代码,它可以正常工作

[Event(name="complete",type="flash.events.Event")]  
[Event(name="status",type="flash.events.StatusEvent")]  
public class LocalFileLoader extends EventDispatcher  
{  
    public function LocalFileLoader()  
    {}      
    private var file:FileReference;// = FileReference(event.target);  
    private var list:FileReferenceList; 
    public var p2pSharedObject:P2PSharedObject = new P2PSharedObject();  
    public function browseFileSystem():void {  
        file = new FileReference();  
        list = new FileReferenceList();  
        list.addEventListener(Event.SELECT, selectHandler);  
        list.browse();  
    }  
    protected function selectHandler(event:Event):void {  
        for each ( file in list.fileList ){  
            file.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);    
            file.addEventListener(ProgressEvent.PROGRESS, progressHandler);  
            file.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);         
            file.addEventListener(Event.COMPLETE, completeHandler);  
            writeText(file.name+" | "+file.size);  
            file.load();      
        }   
    }   
    protected function securityErrorHandler(event:SecurityErrorEvent):void {  
        writeText("securityError: " + event);  
    }  
    protected function completeHandler(event:Event):void {  
        writeText("completeHandler");  
        p2pSharedObject = new P2PSharedObject();  
        p2pSharedObject.size = file.size;  
        p2pSharedObject.packetLength = Math.floor(file.size/32000)+1;  
        p2pSharedObject.data = file.data;  
        p2pSharedObject.chunks = new Object();  
        var desc:Object = new Object();  
        desc.totalChunks = p2pSharedObject.packetLength+1;  
        desc.name = file.name;  
        p2pSharedObject.chunks[0] = desc;  
        for(var i:int = 1;i<p2pSharedObject.packetLength;i++){  
            p2pSharedObject.chunks[i] = new ByteArray();  
            p2pSharedObject.data.readBytes(p2pSharedObject.chunks[i],0,32000);  
        }  
        // +1 last packet  
        p2pSharedObject.chunks[p2pSharedObject.packetLength] = new ByteArray();  
        p2pSharedObject.data.readBytes(p2pSharedObject.chunks[i],0,p2pSharedObject.data.bytesAvailable);  
        p2pSharedObject.packetLength+=1;  
        writeText("----- p2pSharedObject -----");  
        writeText("packetLenght: "+(p2pSharedObject.packetLength));  
        dispatchEvent(new Event(Event.COMPLETE));  
    }  
    protected function writeText(str:String):void{  
        var e:StatusEvent = new StatusEvent(StatusEvent.STATUS,false,false,"status",str);  
        dispatchEvent(e);  
    }  
}  

发送舞台上的视频后面的sender.mxml代码

private function init() : void {  
    fileLoader = new LocalFileLoader();  
    fileLoader.addEventListener(Event.COMPLETE, fileLoaded);  
    fileShare = new P2PFileShare();  
    fileShare.addEventListener(StatusEvent.STATUS,
            function(event:StatusEvent):void {  
                writeText(event.level);  
            });    
    fileShare.connect();  
}    

private function fileLoaded ( event:Event ) : void {  
    writeText("fileLoaded");  
    if (fileShare.connected) {  
        fileShare.p2pSharedObject = fileLoader.p2pSharedObject;  
        fileShare.p2pSharedObject.lastIndexBegin = 0;  
        fileShare.p2pSharedObject.lastIndexEnd = fileShare.p2pSharedObject.packetLength-1;  
        fileShare.updateHaveObjects();  
    }  
    setupVideo();  
    // PLAY  
    ns.play(null);  

    ns.appendBytesAction(NetStreamAppendBytesAction.RESET_BEGIN);               
    ns.appendBytes(fileLoader.p2pSharedObject.data);  
    video.attachNetStream(ns);  
}  

private function setupVideo():void{  
    var nc:NetConnection = new NetConnection();  
    nc.connect(null);  
    ns = new NetStream(nc);  
    ns.client = this;  
    ns.addEventListener(NetStatusEvent.NET_STATUS, 
            function(event:NetStatusEvent):void{  
                writeText("stream: "+event.info.code);  
            });  
    video = new Video();  
    videoComp = new UIComponent();  
    videoComp.addChild(video);  
    this.addElement(videoComp);  
}  

然后在此之后,file.name和file.size转到mxml页面,视频显示在具有名称和文件大小的舞台上 但是我想用FileReferenceList类做同样的事情,我从最近2周解决了这个问题,但是不能,,,引导我,,,我谷歌这么多次,但没有具体的答案 问候
Ammad Khan

1 个答案:

答案 0 :(得分:0)

您可以使用FileReferenceList类的fileList property来访问所选文件。然后单独加载()列表中的每个FileReference:

private var list:FileReferenceList;

public function browseFileSystem():void {

    list = new FileReferenceList();  
    list.addEventListener(Event.SELECT, selectHandler);  
    list.browse();  
} 

protected function selectHandler(event:Event):void {  

    for each ( var file:FileReference in list.fileList) {
        file.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);  
        file.addEventListener(ProgressEvent.PROGRESS, progressHandler);  
        file.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);  
        file.addEventListener(Event.COMPLETE, completeHandler);  
        writeText(file.name+" | "+file.size);           
        file.load(); 
    } 
}