开始不清楚, 一直尝试想load图片一样加载一个swftools转换的swf,然后在尝试转换成movieclip的时候,总是报错, avmiMovieClip 不能转换成movieclip之类的。

但为什么有的swf可以轻松转换成movieclip呢?

 

后面我明白这两种movieclip根本就是不同存储格式了,—— 虽然都是swf后缀

 

关于movieclip ,我一直不太明白,其实它相当的有用。 

 

package
{
import flash.display.Loader;
import flash.errors.EOFError;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.events.SecurityErrorEvent;
import flash.net.URLRequest;
import flash.net.URLStream;
import flash.system.ApplicationDomain;
import flash.system.LoaderContext;
import flash.utils.ByteArray;
import flash.utils.Endian;

import mx.controls.Alert;


public class ForcibleLoader
{
    public function ForcibleLoader()//loader:Loader
    {
        this.loader = new Loader();
        
        _stream = new URLStream();
        _stream.addEventListener(Event.COMPLETE, completeHandler);
        _stream.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
        _stream.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
    }
    
    private var _loader:Loader;
    private var _stream:URLStream;
    private var _callback:Function;
    
    public function get loader():Loader
    {
        return _loader;
    }
    
    public function set loader(value:Loader):void
    {
        _loader = value;
    }
    
    public function load(url:String, callback:Function):void
    {
        var request:URLRequest = new URLRequest(url);
        this._callback = callback;
        _stream.load(request);
        
//        var urlRequest:URLRequest = new URLRequest("adobe1231.swf");
//        var urlLoader:URLLoader=new URLLoader();
//        urlLoader.addEventListener(Event.COMPLETE,completeHandler);
//        urlLoader.dataFormat=URLLoaderDataFormat.BINARY;
        

    }
        
    private function completeHandler(event:Event):void
    {
        var inputBytes:ByteArray = new ByteArray();
        _stream.readBytes(inputBytes);
//        _stream.close();
        inputBytes.endian = Endian.LITTLE_ENDIAN;
        
        if (isCompressed(inputBytes)) {
            uncompress(inputBytes);
        }
        
        var version:uint = uint(inputBytes[3]);
        
//        if (version < 9) {
//            updateVersion(inputBytes, 9);
//        }
//        if (version > 7) {
//            flagSWF9Bit(inputBytes);
//        }
//        else {
//            insertFileAttributesTag(inputBytes);
//        }
        
        if (version <= 9) {
            if (version == 8 || version == 9)
            {
                flagSWF9Bit(inputBytes);
            }else if (version <= 7)
            {
                insertFileAttributesTag(inputBytes);
            }
            updateVersion(9, inputBytes);
        }
        
        this.loader = new Loader();
        loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loadProgress)
        loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onError)
        loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete)
        
        
                var context:LoaderContext = new LoaderContext();
                context.applicationDomain = new ApplicationDomain(ApplicationDomain.currentDomain);
                
        loader.loadBytes(inputBytes, context);//new LoaderContext(true, ApplicationDomain.currentDomain));
        //loader.addEventListener(Event.COMPLETE, loaderComplete)
    }
    
    private function loadComplete(evt:Event):void
    {
        Alert.show("sssssssssssss");
        //_callback(loader.content as MovieClip);
    }
    private function loadProgress(evt:ProgressEvent):void
    {
        trace("  ProgressEvent  : " +evt.bytesLoaded + "  / " +evt.bytesTotal);
        if(evt.bytesLoaded == evt.bytesTotal) {
            
            //_callback(loader.content as MovieClip);
        }
    }
    private function onError(evt:IOErrorEvent):void
    {
        Alert.show("  ProgressEvent  ");
    }
    
    
    private function isCompressed(bytes:ByteArray):Boolean
    {
        return bytes[0] == 0x43;
    }
    
    private function uncompress(bytes:ByteArray):void
    {
        var cBytes:ByteArray = new ByteArray();
        cBytes.writeBytes(bytes, 8);
        bytes.length = 8;
        bytes.position = 8;
        cBytes.uncompress();
        bytes.writeBytes(cBytes);
        bytes[0] = 0x46;
        cBytes.length = 0;
    }
    
//    private function getBodyPosition(bytes:ByteArray):uint
//    {
//        var result:uint = 0;
//        
//        result += 3; // FWS/CWS
//        result += 1; // version(byte)
//        result += 4; // length(32bit-uint)
//        
//        var rectNBits:uint = bytes[result] >>> 3;
//        result += (5 + rectNBits * 4) / 8; // stage(rect)
//        
//        result += 2;
//        
//        result += 1; // frameRate(byte)
//        result += 2; // totalFrames(16bit-uint)
//        
//        return result;
//    }
    
        public function flagSWF9Bit(bytes:ByteArray):void
        {
            var pos:int = findFileAttributesPosition(getBodyPosition(bytes), bytes);
            
            if (pos != -1) {
                bytes[pos + 2] |= 0x08;
            }
            else {
                insertFileAttributesTag(bytes);
            }
        }
        
        private function findFileAttributesPosition(offset:uint, bytes:ByteArray):int
        {
            bytes.position = offset;
            
            try {
                for (;;) {
                    var byte:uint = bytes.readShort();
                    var tag:uint = byte >>> 6;
                    if (tag == 69) {
                        return bytes.position - 2;
                    }
                    var length:uint = byte & 0x3f;
                    if (length == 0x3f) {
                        length = bytes.readInt();
                    }
                    bytes.position += length;
                }
            }
            catch (e:EOFError) {
            }
            
            return -1;
        }
        private function insertFileAttributesTag(bytes:ByteArray):void
        {
            var pos:uint = getBodyPosition(bytes);
            var afterBytes:ByteArray = new ByteArray();
            afterBytes.writeBytes(bytes, pos);
            bytes.length = pos;
            bytes.position = pos;
            bytes.writeByte(0x44);
            bytes.writeByte(0x11);
            bytes.writeByte(0x08);
            bytes.writeByte(0x00);
            bytes.writeByte(0x00);
            bytes.writeByte(0x00);
            bytes.writeBytes(afterBytes);
            afterBytes.length = 0;
        }
        private function getBodyPosition(bytes:ByteArray):uint
        {
            var result:uint = 0;
            
            result += 3; // FWS/CWS
            result += 1; // version(byte)
            result += 4; // length(32bit-uint)
            
            var rectNBits:uint = bytes[result] >>> 3;
            result += (5 + rectNBits * 4) / 8; // stage(rect)
            
            result += 2;
            
            result += 1; // frameRate(byte)
            result += 2; // totalFrames(16bit-uint)
            
            return result;
        }
        
        public function updateVersion(version:uint, b:ByteArray):void
        {
            b[3] = version;
        }
    
//    private function findFileAttributesPosition(offset:uint, bytes:ByteArray):uint
//    {
//        bytes.position = offset;
//        
//        try {
//            for (;;) {
//            var byte:uint = bytes.readShort();
//            var tag:uint = byte >>> 6;
//            if (tag == 69) {
//            return bytes.position - 2;
//            }
//            var length:uint = byte & 0x3f;
//            if (length == 0x3f) {
//            length = bytes.readInt();
//            }
//            bytes.position += length;
//            }
//        }
//        catch (e:EOFError) {
//        }
//        
//        return NaN;
//    }
    
//    private function flagSWF9Bit(bytes:ByteArray):void
//    {
//        var pos:uint = findFileAttributesPosition(getBodyPosition(bytes), bytes);
//        if (!isNaN(pos)) {
//            bytes[pos + 2] |= 0x08;
//        }
//    }
//    
//    private function insertFileAttributesTag(bytes:ByteArray):void
//    {
//        var pos:uint = getBodyPosition(bytes);
//        var afterBytes:ByteArray = new ByteArray();
//        afterBytes.writeBytes(bytes, pos);
//        bytes.length = pos;
//        bytes.position = pos;
//        bytes.writeByte(0x44);
//        bytes.writeByte(0x11);
//        bytes.writeByte(0x08);
//        bytes.writeByte(0x00);
//        bytes.writeByte(0x00);
//        bytes.writeByte(0x00);
//        bytes.writeBytes(afterBytes);
//        afterBytes.length = 0;
//    }
//    
//    private function updateVersion(bytes:ByteArray, version:uint):void
//    {
//        bytes[3] = version;
//    }
    
    private function ioErrorHandler(event:IOErrorEvent):void
    {
        loader.contentLoaderInfo.dispatchEvent(event.clone());
    }
    
    private function securityErrorHandler(event:SecurityErrorEvent):void
    {
        loader.contentLoaderInfo.dispatchEvent(event.clone());
    }
}
}
View Code

相关文章:

  • 2021-11-11
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-21
  • 2021-08-19
猜你喜欢
  • 2021-05-19
  • 2021-09-30
  • 2021-07-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-06
  • 2021-08-29
相关资源
相似解决方案