ashier.com flash / flex / air development
rss rss comment

Archive for the ‘Flex’ Category

Donald Martinez extended the datagrid component that will allow you to implicitly page contents in a data grid… You can view his post and download the component or view the demo by clicking this link.

You will need to download the as3corelib classes for this to work.

JSONManager is a wrapper class to simplify JSON service calls. It has a feature that allows you to call service's asynchronously.

Actionscript:
  1. package lib.ashier.managers {
  2.  
  3.     import com.adobe.serialization.json.JSON;
  4.  
  5.     import flash.events.EventDispatcher;
  6.     import flash.utils.Dictionary;
  7.  
  8.     import lib.ashier.events.JSONManagerEvent;
  9.  
  10.     import mx.rpc.events.FaultEvent;
  11.     import mx.rpc.events.ResultEvent;
  12.     import mx.rpc.http.HTTPService;
  13.  
  14.     public class JSONManager extends EventDispatcher{
  15.  
  16.         function JSONManager():void {
  17.             super();
  18.             initializeJSON();
  19.         }
  20.  
  21.         private var service:HTTPService = new HTTPService();
  22.         protected function initializeJSON():void {
  23.             // INITIIALIZE
  24.         }
  25.  
  26.         private function onJSONResult(event:ResultEvent):void {
  27.             if(event.result != "") {
  28.                 var obj:Object = Object(JSON.decode(String(event.result)));
  29.                 obj.url = event.target.url;
  30.                 delete serviceDict[obj.url];
  31.             }else {
  32.                 onJSONFault(new FaultEvent(FaultEvent.FAULT));
  33.                 delete serviceDict[url];
  34.                 return;
  35.             }
  36.             dispatchEvent(new JSONManagerEvent(JSONManagerEvent.RESULT, obj));
  37.         }
  38.  
  39.         private function onJSONFault(event:FaultEvent):void  {
  40.             dispatchEvent(new JSONManagerEvent(JSONManagerEvent.FAULT, {message:((event.fault) ? event.fault.faultString : "Unknown JSON Fault")}));
  41.         }
  42.  
  43.         private var serviceDict:Dictionary = new Dictionary();
  44.         public function send(params:Object = null):void {
  45.             var parameters:String = JSON.encode(params);
  46.  
  47.             if(!serviceDict[_url]) {
  48.  
  49.                 service = new HTTPService();
  50.                 service.useProxy = false;
  51.                 service.contentType = _contentType;
  52.                 service.method = _method;
  53.                 service.url = _url;
  54.                 service.addEventListener(ResultEvent.RESULT, onJSONResult);
  55.                 service.addEventListener(FaultEvent.FAULT, onJSONFault);
  56.  
  57.                 serviceDict[_url] = service
  58.             }
  59.  
  60.             if(params) {
  61.                 service.send(parameters);
  62.             }else {
  63.                 service.send();
  64.             }
  65.             dispatchEvent(new JSONManagerEvent(JSONManagerEvent.SENDING, null));
  66.         }
  67.  
  68.         private var _contentType:String = "text/html";
  69.         public function set contentType(s:String):void {
  70.             _contentType = s;
  71.         }
  72.  
  73.         public function get contentType():String {
  74.             return _contentType;
  75.         }
  76.  
  77.         private var _method:String = "POST";
  78.         public function set method(s:String):void {
  79.             _method = s;
  80.         }
  81.  
  82.         public function get method():String {
  83.             return _method;
  84.         }
  85.  
  86.         private var _url:String = "";
  87.         public function set url(s:String):void {
  88.             _url = s;
  89.         }
  90.  
  91.         public function get url():String {
  92.             return _url;
  93.         }
  94.  
  95.     }
  96.  
  97. }

JSONManagerEvent

Actionscript:
  1. package lib.ashier.events {
  2.  
  3.     import lib.ashier.core.interfaces.IEvent;
  4.     import flash.events.Event;
  5.  
  6.     public class JSONManagerEvent extends Event implements IEvent {
  7.  
  8.  
  9.         public static const SENDING:String = "sending";
  10.         public static const RESULT:String = "result";
  11.         public static const FAULT:String = "fault";
  12.  
  13.         private var value:Object = new Object();
  14.  
  15.         public function JSONManagerEvent(type:String, value:Object) {
  16.                 super(type);
  17.                 this.value = value;
  18.         }
  19.  
  20.         public function get data():Object {
  21.             return this.value;
  22.         }
  23.  
  24.         override public function clone():Event {
  25.             return new JSONManagerEvent(type, value);
  26.         }
  27.  
  28.     }
  29. }

I think this is the best flex development resource so far.

http://blog.flexexamples.com/

I've been trying to develop an ID3 parser and here's what i got so far.

I've update the ID3Parser. It is much faster now.

Actionscript:
  1. package libs.ashier.id3 {
  2.  
  3.     package libs.ashier.id3 {
  4.  
  5.     import flash.events.EventDispatcher;
  6.     import flash.filesystem.File;
  7.     import flash.filesystem.FileMode;
  8.     import flash.filesystem.FileStream;
  9.     import flash.utils.ByteArray;
  10.     import flash.utils.Dictionary;
  11.     import flash.utils.clearInterval;
  12.     import flash.utils.setInterval;
  13.  
  14.     public class ID3Parser extends EventDispatcher{
  15.  
  16.         public function ID3Parser():void {}
  17.  
  18.         /**
  19.          * Bit 7 in the 'ID3v2 flags' indicates whether or not
  20.          * unsynchronisation is applied on all frames.
  21.          */
  22.         private var unsynchronisation:uint;
  23.  
  24.         /**
  25.          * The second bit (bit 6) indicates whether or not the header is
  26.          * followed by an extended header.
  27.          * A set bit indicates the presence of an extended header.
  28.          */
  29.         private var extendedHeader:uint;
  30.  
  31.         /**
  32.          * The third bit (bit 5) is used as an 'experimental indicator'. This
  33.          * flag SHALL always be set when the tag is in an experimental stage.
  34.          */
  35.         private var experimental:uint;
  36.  
  37.         /**
  38.          * FileStream / Bytes
  39.          */
  40.         private var fs:FileStream;
  41.  
  42.         /**
  43.          * Id3 Version
  44.          */
  45.         private var version:Number;
  46.  
  47.         /**
  48.          * Available Genres
  49.          */
  50.         private var genres:Array = [
  51.                                 "Blues", "Classic Rock", "Country", "Dance", "Disco", "Funk",
  52.                                 "Grunge", "Hip-Hop", "Jazz", "Metal", "New Age", "Oldies", "Other",
  53.                                 "Pop", "R&B", "Rap", "Reggae", "Rock", "Techno", "Industrial",
  54.                                 "Alternative", "Ska", "Death Metal", "Pranks", "Soundtrack",
  55.                                 "Euro-Techno", "Ambient", "Trip-Hop", "Vocal", "Jazz+Funk", "Fusion",
  56.                                 "Trance", "Classical", "Instrumental", "Acid", "House", "Game",
  57.                                 "Sound Clip", "Gospel", "Noise", "Alt. Rock", "Bass", "Soul",
  58.                                 "Punk", "Space", "Meditative", "Instrum. Pop", "Instrum. Rock",
  59.                                 "Ethnic", "Gothic", "Darkwave", "Techno-Indust.", "Electronic",
  60.                                 "Pop-Folk", "Eurodance", "Dream", "Southern Rock", "Comedy",
  61.                                 "Cult", "Gangsta", "Top 40", "Christian Rap", "Pop/Funk", "Jungle",
  62.                                 "Native American", "Cabaret", "New Wave", "Psychadelic", "Rave",
  63.                                 "Showtunes", "Trailer", "Lo-Fi", "Tribal", "Acid Punk", "Acid Jazz",
  64.                                 "Polka", "Retro", "Musical", "Rock & Roll", "Hard Rock", "Folk",
  65.                                 "Folk/Rock", "National Folk", "Swing", "Fusion", "Bebob", "Latin",
  66.                                 "Revival", "Celtic", "Bluegrass", "Avantgarde", "Gothic Rock",
  67.                                 "Progress. Rock", "Psychadel. Rock", "Symphonic Rock", "Slow Rock",
  68.                                 "Big Band", "Chorus", "Easy Listening", "Acoustic", "Humour",
  69.                                 "Speech", "Chanson", "Opera", "Chamber Music", "Sonata", "Symphony",
  70.                                 "Booty Bass", "Primus", "Porn Groove", "Satire", "Slow Jam",
  71.                                 "Club", "Tango", "Samba", "Folklore", "Ballad", "Power Ballad",
  72.                                 "Rhythmic Soul", "Freestyle", "Duet", "Punk Rock", "Drum Solo",
  73.                                 "A Capella", "Euro-House", "Dance Hall", "Goa", "Drum & Bass",
  74.                                 "Club-House", "Hardcore", "Terror", "Indie", "BritPop", "Negerpunk",
  75.                                 "Polsk Punk", "Beat", "Christian Gangsta Rap", "Heavy Metal",
  76.                                 "Black Metal", "Crossover", "Contemporary Christian", "Christian Rock",
  77.                                 "Merengue", "Salsa", "Thrash Metal", "Anime", "Jpop", "Synthpop"
  78.                                 ];
  79.  
  80.  
  81.         /**
  82.          * Get ID3 Profile
  83.          *
  84.          */
  85.         private function getID3Profile():void {
  86.             fs = new FileStream();
  87.             try {
  88.                 fs.open(_file, FileMode.READ);
  89.             }catch (e:Error) {}
  90.             if(_file) {
  91.                 clearInterval(interval);
  92.                 interval = setInterval(checkID3, 10, fs);
  93.             }
  94.         }
  95.  
  96.         /**
  97.          * ID3 Flag
  98.          */
  99.         private var flags:uint;
  100.         private var interval:uint;
  101.         private var interval2:uint;
  102.  
  103.         /**
  104.          * Check if ID3 is Valid.
  105.          * @param fs
  106.          *
  107.          */
  108.         public function checkID3(fs:FileStream):void {
  109.             clearInterval(interval);
  110.             fs.position = 0;
  111.             var type:String = fs.readUTFBytes(3);
  112.             if(type.toLocaleUpperCase() == "ID3") {
  113.                 version = fs.readByte() + fs.readByte()/100;
  114.                 if(version>= 3) {
  115.                     frameIdSize = 4;
  116.                 }
  117.  
  118.                 flags = fs.readByte();
  119.                 unsynchronisation = flags>> 7;
  120.                 extendedHeader = flags>> 6 && 01;
  121.                 experimental = flags>> 5 && 001;
  122.                 clearInterval(interval2);
  123.                 interval2 = setInterval(readFrames, 10);
  124.             }else {
  125.                 dispatchEvent(new ID3ParserEvent(ID3ParserEvent.ERROR, {message:"Not a valid ID3."}));
  126.             }
  127.         }
  128.  
  129.         /**
  130.          * Declared ID3v2 frames
  131.          */
  132.         private var frames:Object;
  133.  
  134.         /**
  135.          * First FrameId Size. Default is 3.
  136.          */
  137.         private var frameIdSize:uint = 3;
  138.  
  139.         /**
  140.          * Parse all types of frames in an ID3.
  141.          *
  142.          */
  143.  
  144.         private var length:int = 0;
  145.         private var lastFSPosition:int = 0;
  146.  
  147.         private function readFrames():void {
  148.             clearInterval(interval2);
  149.             if(!frames) {
  150.                 frames = new Object();
  151.             }
  152.             length = convertSynchsafe(fs.readUnsignedInt()) + 10;
  153.             parseFrames();
  154.             lastFSPosition = fs.position;
  155.             fs.close();
  156.             dispatchEvent(new ID3ParserEvent(ID3ParserEvent.ID3, ID3Info));
  157.         }
  158.  
  159.          private var regEx:RegExp = new RegExp("" +
  160.                         "/AENC|APIC|PIC|" +
  161.                         "COMM|COMR|" +
  162.                         "ENCR|EQUA|ETCO|" +
  163.                         "GEOB|GRID|" +
  164.                         "IPLS|" +
  165.                         "LINK|" +
  166.                         "MCDI|MLLT|" +
  167.                         "OWNE|" +
  168.                         "PRIV|PCNT|POPM|POSS|" +
  169.                         "RBUF|RVAD|RVRB|" +
  170.                         "SYLT|SYTC|" +
  171.                         "T*" +
  172.                         "UFID|USER|USLT|" +
  173.                         "W*/", "ig");
  174.  
  175.         private function parseFrames():void {
  176.             var id:String = "";
  177.             var size:uint = 0;
  178.             if(fs.position <length) {
  179.                 try {
  180.                     id = fs.readUTFBytes(frameIdSize);
  181.                     size = fs.readUnsignedInt();
  182.                     if (version>= 3) {
  183.                         fs.readByte();
  184.                         fs.readByte();
  185.                     }
  186.                     if(id.match(regEx)) {
  187.                         var obj:Object = new Object();
  188.                         obj.encoding = fs.readByte();
  189.                         obj.text = fs.readUTFBytes(size - 1);
  190.                         frames[id] = obj;
  191.                     }
  192.                     parseFrames();
  193.                 }catch(e:Error) {}
  194.             }
  195.         }
  196.  
  197.         /**
  198.          * Get value from a ID3 frame.
  199.          * @param frameType
  200.          * @return
  201.          *
  202.          */
  203.         public function getFrameType(frameType:String):Object {
  204.             return frames[frameType];
  205.         }
  206.         private var ID3Frames:Object = {GENRE:”TCON”,ALBUM:”TALB”,ARTIST:”TPE1″,COMMENT:”COMM”,TITLE:”TIT2″,TRACK_NUMBER:”TRCK”,LYRICIST:”TOLY”,YEAR:”TYER”};
  207.         /**
  208.          * Returns ID3Info.
  209.          * @return
  210.          *
  211.          */
  212.         public function get ID3Info():Object {
  213.             if(frames) {
  214.                 var genreType:String = "";
  215.                 if(frames[ID3Frames.GENRE]) {
  216.                     try {
  217.                         genreType = frames[ID3Frames.GENRE].text;
  218.                         if(genreType.indexOf("(") != -1) {
  219.                             genreType = genreType.substr(1, genreType.indexOf(")") - 1);
  220.                             genreType = genres[Number(genreType)];
  221.                         }
  222.                     }catch(e:Error) {
  223.                         genreType = "";
  224.                     }
  225.                 }
  226.                 return {
  227.                             album:frames[ID3Frames.ALBUM] ? frames[ID3Frames.ALBUM].text : "",
  228.                             artist:frames[ID3Frames.ARTIST] ? frames[ID3Frames.ARTIST].text : "",
  229.                             comment:frames[ID3Frames.COMMENT] ? frames[ID3Frames.COMMENT].text : "",
  230.                             genre:genreType,
  231.                             title:frames[ID3Frames.TITLE] ? frames[ID3Frames.TITLE].text : "",
  232.                             track:frames[ID3Frames.TRACK_NUMBER] ? frames[ID3Frames.TRACK_NUMBER].text : "",
  233.                             lyrics:frames[ID3Frames.LYRICIST] ? frames[ID3Frames.LYRICIST].text : "",
  234.                             year:frames[ID3Frames.YEAR] ? frames[ID3Frames.YEAR].text : ""
  235.                         };
  236.             }
  237.             return new Object();
  238.         }
  239.  
  240.         private function convertSynchsafe(value:uint):uint {
  241.             return (value & 127) + 128 * ((value>> 8) & 127) + 16384 * ((value>>16) & 127) + 2097152 * ((value>> 24) & 127);
  242.         }
  243.  
  244.         private var _file:File;
  245.         public function set file(f:File):void {
  246.             _file = f;
  247.             getID3Profile();
  248.         }
  249.  
  250.         public function get file():File {
  251.             return _file;
  252.         }
  253.     }
  254. }

ID3 Parser Event

Actionscript:
  1. package libs.ashier.id3 {
  2.  
  3.     import flash.events.Event;
  4.  
  5.     public class ID3ParserEvent extends Event {
  6.  
  7.         public static const ERROR:String = "error";
  8.         public static const ID3:String = "id3";
  9.  
  10.         public function ID3ParserEvent(type:String, value:Object = null) {
  11.             super(type);
  12.             this.data = value;
  13.         }
  14.  
  15.         public var data:Object;
  16.         override public function clone():Event {
  17.             return new ID3ParserEvent(type, data);
  18.         }
  19.  
  20.     }
  21. }

Usage:

Actionscript:
  1. private var file:File = new File();
  2. private function initialize():void {
  3.         file.browseForDirectory("Browse...");
  4.     file.addEventListener(Event.SELECT, onDirectory);
  5. }
  6.  
  7. private function onDirectory(event:Event):void {
  8.      var files:Array = File(event.target).getDirectoryListing();
  9.          for( var i:Number = 0; i <files.length; i++ ){
  10.         var file:File = File(files[i]);
  11.         var prof:ID3Parser = new ID3Parser();
  12.         prof.addEventListener(ID3ParserEvent.ID3, onID3);
  13.         prof.file = file;
  14.          }
  15. }
  16. private function onID3(event:ID3ParserEvent):void {
  17.         //event.data
  18. }

This is written for an AIR application.

From Adobe Labs
Adobe® LiveCycle Data Services 2.5 is the next generation of Flex Data Services. The name change reflects an important expansion in the use of these valuable services. In addition to serving the needs of both Flex and Ajax developers, LiveCycle Data Services will provide enhanced integration with Adobe’s other LiveCycle server products for document and process management, enabling business to create new ways to engage and reach customers.

You can view it here

sorry... I have to pull the flex blog for now... I'll be updating it and hopefully it will run in parallel with this blog soon.

I have created a blog written in Adobe Flex 2. This blog uses wordpress framework, and AMFPHP for remoting The blog is still in beta stage. A lot of functionalities is still not integrated but hopefully a lot of updates will be added in the next few days.