Search code examples
actionscript-3arraysanimated-gif

Flash-Animated-GIF-Library


I'm trying to use the Flash-Animated-GIF-Library. It serves to load an animated gif. It does it with the fileReference class, where you have to browse your folders, choose an animated gif and then it'll show it on the stage. I need the animated gif to show without that browsing part. Is it in anyway possible to use that class to load animated gifs directly, the same way you'd load and show an image with the Loader class? If so - how?


Solution

  • Yes, you have 2 options.

    1. Use the Loader or URLLoader class. Example1, Example 2 (get bytearray)
    2. Embed the image and get ByteArrayAsset. Example

    The minimal code for option1 (Loader):

    protected function handleCreationComplete(event:FlexEvent):void
    {
        var loader:Loader = new Loader();        
        loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderComplete);
        loader.load(new URLRequest("yourgif.gif"));
    }
    
    private function loaderComplete(event:Event):void
    {
        var loaderInfo:LoaderInfo = LoaderInfo(event.target);
        var byteArray:ByteArray = loaderInfo.bytes;        
        player.loadBytes(byteArray);
    }
    

    The minimal code for option1 (URLLoader):

    protected function handleCreationComplete(event:FlexEvent):void
    {
        var loader:URLLoader = new URLLoader();
        loader.dataFormat = URLLoaderDataFormat.BINARY;
        loader.addEventListener(Event.COMPLETE, loaderComplete);
        loader.load(new URLRequest("yourgif.gif"))
    }
    
    private function loaderComplete(event:Event):void
    {
        player.loadBytes(event.target.data);
    }
    

    And for option2:

    [Embed(source="yourgif.gif",mimeType="application/octet-stream")]
    public var YourGif:Class;
    
    protected function handleCreationComplete(event:FlexEvent):void
    {
        var byteArrayAsset:ByteArrayAsset = new YourGif();
        player.loadBytes(byteArrayAsset);
    
        // should work, too
        //player.loadBytes(new YourGif() as ByteArray);
    }