I 've been playing around with Flex modules lately and thought I 'd post this. it's pretty basic, but it is kind of a "My first module" type experiment. I tried to show a few different things including: calling a module's methods from the parent application as well as setting properties in the parent application from the loaded module.
If you haven't looked at modules in flex yet, I highly encourage you to check out the flex Doc team blog at http://blogs.adobe.com/flexdoc, where you can find their latest version of the "Creating modular applications" chapter (blog entry, PDF ).
<? XML version = "1.0" encoding = "UTF-8" ?>
< MX: Application Xmlns: MX = "Http://www.adobe.com/2006/mxml" >
< MX: script >
<! [CDATA [
Import MX. Events. videoevent;
[Bindable]
Private var moduletitle: string;
Private var VM: videomodule;
Private function Init (): void {
Vm = videomodule (m1.child );
Moduletitle = VM. getmoduletitle ();
}
Private function stopvideo (): void {
VM. stopvideo ();
}
Private function playpausevideo (): void {
VM. playpausevideo ();
}
]>
</MX: script>
< MX: Panel ID = "Panel" Title = "Module: {moduletitle }" >
< MX: moduleloader URL = "Videomodule.swf" ID = "M1" Ready = "Init ()" />
< MX: controlbar >
< MX: button Label = "Play/pause" Click = "Playpausevideo ()" />
< MX: button Label = "Stop" Click = "Stopvideo ()" />
< MX: Spacer Width = "100%" />
< MX: Label ID = "Playheadtime" Fontweight = "Bold" />
</ MX: controlbar >
</ MX: Panel >
</MX: Application>
Videomodule. mxml <? XML version = "1.0" encoding = "UTF-8" ?>
< MX: Module Xmlns: MX = "Http://www.adobe.com/2006/mxml" Width = "100%" Height = "100%" >
< MX: script >
<! [CDATA [
Public Function getmoduletitle (): String {
Return "video module ";
}
/* Stop the video playback .*/
Public Function stopvideo (): void {
Videodisplay. Stop ();
}
/* If the video is currently playing, pause playback. Otherwise, Resume playback .*/
Public Function playpausevideo (): void {
If (videodisplay. Playing ){
Videodisplay. Pause ();
} Else {
Videodisplay. Play ();
}
}
Private function updatevideotime (): void {
/* If the playheadtime is 0, the dateformatter returns an empty string.
To work around this we can default the time to 10 ms if the playheadtime
Is zero .*/
VaR ptime: Date = new date (videodisplay. playheadtime * 1000 | 10 );
VaR tTime: Date = new date (videodisplay. totaltime * 1000 );
Parentapplication. playheadtime. Text = dateformatter. Format (ptime) + "/" + dateformatter. Format (tTime );
}
]>
</ MX: script >
<MX: dateformatterID= "Dateformatter"Formatstring= "NN: SS" />
mx: videodisplay id =" videodisplay " source =" http://www.helpexamples.com/flash/video/cuepoints.flv " playheadupdate =" updatevideotime () " />
mx: module >