Here are a couple of snippets i've used when working on my chat application.

1. Add sound to your Flex application:

[Embed(source="media/audibles/chimeup.mp3")]
[Bindable]
public var chimeup:Class;
public var snd_chimeup:Sound = new chimeup() as Sound;
public var sndChannel_chimeup:SoundChannel;
public function playChimeup():void {
sndChannel_chimeup = snd_chimeup.play();
}


2. Making some code be executed at given intervals:

public function ShortTimer():void
{
var minuteTimer:Timer = new Timer(300000, 0);
minuteTimer.addEventListener(TimerEvent.TIMER, onTick);
minuteTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
minuteTimer.start();
}

public function onTick(evt:TimerEvent):void
{
<!-- Put here the code you need to be executed --->
}

public function onTimerComplete(evt:TimerEvent):void
{
ShortTimer();
}

Things to be noted here:
  • 300000 - is the number of milliseconds between code executions
  • onTick - is the function that will be executed at the given interval
  • onTimerComplete - the function that will be executed when the time finishes. Because we call ShortTimer() there it will mean that the timer will be reactivated.
  • Now all you need is to add creationComplete="ShortTimer()" to your mx:Application


  • 3. Load CSS at runtime

    Ruben Swieringa have a very nice tool - CSSLoader that allow you do that.
    But there is a "spoon of tar". There seem to be cases when the CSS from the external file will not be applied to your application parts. So far it didn't work for me in the following cases:
  • setting the CSS for DataGridColumn when the CSS for DataGrid has been set
  • I have an Accordion with 2 VBox'es. In the second VBox there is a HBox that have inside a LinkButton and a CheckBox. Well.. setting the CSS for LinkButton and CheckBox from the external file does not work.


  • 4. Loading configuration data at runtime

    Using something like
    var request:URLRequest = new URLRequest('config.xml?' + Math.random());
    var loader:URLLoader = new URLLoader();
    loader.addEventListener(Event.COMPLETE, onResponse);
    loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorListener);
    loader.load(request);

    will allow you load your configuration and when done the onResponse function will be executed. And the onResponse may look like this:
    [Bindable]
    private var siteURL:String;
    private function onResponse(event:Event):void
    {
    var loader:URLLoader = event.target as URLLoader;
    var _xml:XML = new XML(loader.data);
    var _feeds:XMLList = _xml.item;
    for each (var item:XML in _feeds ) {
    siteURL = item.siteURL.toString();
    }
    }


    OK. That's a start. And here you can see and try the current version of the chat.