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 :
Projects using GIFPlayer Class :
This work is licensed under a Creative Commons Attribution 3.0 License.
Sources and documentation available at http://code.google.com/p/as3gif/