Fun with code (since 2006)

AS3 GIF Player Class 0.3


06.01.07 Posted in Actionscript 3, ByteArray by

After working with the GIF format in ActionScript 3 since a few days, I wanted to be able to load animated GIF's into the player which is not possible for the moment. If you load a GIF file with the flash.display.Loader class you will only get the first frame of the animation.

With this new GIFPlayer class, everything is handled, I have added some methods as you would call on a traditional flash.display.MovieClip

To load an animated GIF you would do :

// we create the GIFPlayer, it plays automatically by default
var myGIFPlayer:GIFPlayer = new GIFPlayer();
// we show it
addChild ( myGIFPlayer );
// we load a gif file
myGIFPlayer.load ( new URLRequest ("animation.gif") );
// you can also load any valid GIF stream (ByteArray) with the loadBytes method (version 0.2)
myGIFPlayer.loadBytes ( gifStream );
// listen for the IOErrorEvent.IO_ERROR event, dispatched when the GIF fails to load
myGIFPlayer.addEventListener ( IOErrorEvent.IO_ERROR, onIOError );
// listen for the GIFPlayerEvent.COMPLETE event, dispatched when GIF is loaded
myGIFPlayer.addEventListener ( GIFPlayerEvent.COMPLETE, onCompleteGIFLoad );
// listen for the FrameEvent.FRAME_RENDERED event, dispatched when a GIF frame is rendered on screen
myGIFPlayer.addEventListener ( FrameEvent.FRAME_RENDERED, onFrameRendered );
// listen for the FileTypeEvent.INVALID event, dispatched when an invalid file is loaded
myGIFPlayer.addEventListener ( FileTypeEvent.INVALID, onInvalidFileLoaded );
// get total frames
myGIFPlayer.totalFrames;
// standard methods
myGIFPlayer.play();
myGIFPlayer.stop();
myGIFPlayer.gotoAndStop(3);
myGIFPlayer.gotoAndPlay(3);

Please test it, and let me know if you got good results. Please don't load huge GIF files, some optimization will be done for this one in the following days.

Online Demo :

A Flash animation should have appeared here, but it seems that your browser has an older version of the Flash Player or it is not installed at all. Please, install the last release of the Flash Player now, then reload this page.

Projects using GIFPlayer Class :

Piterwilson did a funny experiment with this and the GIFAnimation Encoder class.

Sources and documentation available at http://code.google.com/p/as3gif/



161 Responses to “AS3 GIF Player Class 0.3”

  1. lsdev says:

    Hello, is there a progress event handler? Also, my whole .swf freezes until the gif is loaded, is there a way around this?

  2. 942avril says:

    非常棒!很实用!

  3. Tom says:

    Shame that the transparency doesn’t work for every frame.. otherwise a great tool.

    Thanks.

  4. Anthony says:

    Brian McKelvey’s decoder is much faster.
    Thanks Brian,and Thibault.

  5. [...] 在他的Blog里介绍了这个方法,从java GIF encoder [...]

     
  6. ganesh says:

    HI, can any one suggest how to include this class path in flash develop.

    I don’t want to add it in global class path just for one time use.

  7. anil says:

    hi,
    what is wrong this gif in gifPlayer

    http://www.idea-files.com/problemGif/127.gif

    sorry bad english

  8. Lysle says:

    I am also having problems with my gifs being partially transparent past frame 1. I was able to compound the images together, but then bits of frame 1 will appear on the sides of frame 2 and 3.

    I understand that GIFs are optimized by not containing all the data in each frame, but only the data of pixels that have changed. I am attempting to find a way to ‘deoptimize’ them, or as some sources call it ‘coalesce’ each frame. This is not readily apparent in AS3.

    I have many GIF files. If I can find a way to do it via script that I can execute on many GIFs at once and automatically as more GIFs are added to my database, that would be ideal. However at the time I am unable to find even a basic program that coalesces/decompresses/deoptimizes frame 2, 3, etc one at a time.

    Please email me if you have the solution.

  9. Lysle says:

    Reading more about GIF’s here:
    http://giflib.sourceforge.net/whatsinagif/animation_and_transparency.html

    I am thinking that this class has the 3 disposal methods, but the problem is that disposal method 1 is not working correctly. When new frames have areas of transparency vs areas of no data, the transparency should override previous frames data. Instead, areas of transparency are treated like areas of no data, allowing the previous frames to show up through the transparency.

  10. Brian Bedard says:

    Thibalt,

    I’m enjoying this project and was trying to integrate it into an existing project that loads all images via Loader instead of URLLoader. Rather than have to load the gifs again using your code, I was trying to get the bytearray from the loader itself. I can see the bytearray works with my code, but it’s invalid for the loadBytes method you’ve created. Any idea how to make Loader work in the same format? Thanks!

    var loaderInfo:LoaderInfo = LoaderInfo( BitmapLibrary.getImageLoaderInfoFromID( this._image_id ) );

    this.myGIFPlayer.loadBytes( loaderInfo.bytes );

  11. Sodah says:

    Large GIF will flash freeze…..

Leave a Reply

Open Sort Options

Sort comments by:
  • * Applied after refresh

ByteArray.org
Fun with code since 2006.