AS3 browse and load video

2019-09-11 07:45发布

问题:

I'm currently working on a AS3 AIR project which involves allowing the user to browse a video file and load that same video onto the stage. I've managed to allow the user to browse for a video file type and according to my traces it completes loading the video but this is as far as I've got. There is plenty of tutorials which involve how to load video files from local sources or external links but nothing to show me what to do with a browsed file to display it on the stage. Here is the code so far for browsing to the video file:

private function browseVideo():void {
    fileReference = new FileReference();
    fileReference.addEventListener(Event.SELECT, videoFileSelected);

    var videoTypeFilter:FileFilter = new FileFilter("Video files", "*.3g2; *.3gp; *.asf; *.asx; *.avi; *.flv; *.m4v; *.mov; *.mp4; *.mpg; *.rm; *.swf; *.vob; *.wmv;");

    fileReference.browse([videoTypeFilter]);
}

private function videoFileSelected(e:Event):void {
    fileReference.addEventListener(Event.COMPLETE, onVideoFileLoaded);
    fileReference.addEventListener(IOErrorEvent.IO_ERROR, onVideoFileLoadError);
    fileReference.load();
}

function onVideoFileLoaded(e:Event):void {
    var fileReferenceTarget:FileReference = e.target as FileReference;

    var data:ByteArray = fileReferenceTarget["data"];

    fileReference.removeEventListener(Event.COMPLETE, onVideoFileLoaded);
    fileReference.removeEventListener(IOErrorEvent.IO_ERROR, onVideoFileLoadError);

    var videoLoader:Loader = new Loader();
    videoLoader.loadBytes(data);
    videoLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onVideoLoaderComplete);

    trace("video file loaded");
}

function onVideoFileLoadError(e:Event):void {
    fileReference.removeEventListener(Event.COMPLETE, onVideoFileLoaded);
    fileReference.removeEventListener(IOErrorEvent.IO_ERROR, onVideoFileLoadError);

    trace("video file load failed");
}

function onVideoLoaderComplete(e:Event):void {
    var loadedContent:DisplayObject = e.target.content;
    var loader:Loader = e.target.loader as Loader;

    scope.addChild(loader);
}

回答1:

To play a video using AS3 ( Flash ) you can use a Video object on which you can attach a NetStream object, you can also use an FLVPlayback component. For flex, take a look on my answer of this question where I put an example of playing a video stream. And in all cases, I think that you don't need a FileReference object because a File is suffisant to get the path of your local file and then play it with any manner you want.

Take a look on this example :

function browseVideo():void {
    var file:File = new File();
        file.addEventListener(Event.SELECT, videoFileSelected); 
        file.browse([videoTypeFilter]);
}

function videoFileSelected(e:Event):void {
    playVideo(e.currentTarget.nativePath);
}

function playVideo(video_path:String){

    // using a Video + NetStream object
    var nc:NetConnection = new NetConnection();
        nc.connect(null);
    var ns:NetStream = new NetStream(nc);
        ns.client = this;
    var video:Video = new Video();
        video.attachNetStream(ns);
        addChild(video);
    ns.play(video_path);

    // using an FLVPlayback component inserted on the Stage
    flvplayback.load(video_path);
    flvplayback.play();

}

For more details on how to work with video, you can take a look here, you can find all what you need to know about video ( loading videos, supported formats, cue points, ... ).

Hope that can help.