Cannot Trace using Debugger on OSX

Oh good lord, all I wanted to do was trace the output of my flash debug player, yet there was nothing in the usual location. Typically I just run

tail -f ~/Library/Preferences/Macromedia/Flash\ Player/Logs/flashlog.txt

in the Terminal and it spits out everything Flash does. The file didn’t exist on my system, so I couldn’t tail it. The solution turned out to be a missing mm.cfg file.
in ~/Library/Application\ Support/, produce a mm.cfg file with the following content:

TraceOutputFileEnable=1
ErrorReportingEnable=1

This, along with a correct debugger, should produce traces.

fl.browseForFileURL won’t work across hard drives

In JSFL, when you use fl.browseForFileURL it returns a URI pointing to the file or folder you’ve chosen. On OX, however, the reference is wrong when you choose a file on a hard drive other than your primary.

Example: I have a hard drive on my mac named HD2. If I browse to a file on there, the URI returned will be

file:///HD2/myFile.txt

If you then immediately try a

FLfile.exists('file:///HD2/myFile.txt')

it returns false. This is due to OSX’s referencing system. the correct location is

file:///Volumes/HD2/myFile.txt

so all we have to do is add the word “Volumes/” to the string and the reference will be correct.

public static function browseForFileURI( title:String = "", type:String = "open" ):String
{
   var ar:Array = MMExecute( "fl.browseForFileURL('" + type + "','" + title + "');" ).split("/");
   ar[2] = "/Volumes";
   return ar.join("/");
}

Embed a text/txt file in a Flex/AS3 application

This is actually pretty easy, but it’s not something widely known. Here’s the code:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
            layout="absolute"
            creationComplete="trace(text);">
   <mx:Script>
      <![CDATA[
         [Embed( source="myTextFile.txt", mimeType="application/octet-stream" )]
         public var MyText:Class
         public var text:String=new MyText().toString();
      ]]>
   </mx:Script>
</mx:Application>

So what is this doing?
The embed tag works to keep the file in your swf. It then binds it to the Class defined on the next line. Since it’s an unrecognized file format (Flex supports swf, mp3, jpg, gif, png), Flex wouldn’t know what to do with it, so we specify a mimeType — a description of how to handle this file.
Then, we create a new variable and set its contents to a new instance of the Class, then convert it to a string.
Finally, I’ve set the Application parameter creationComplete to perform a trace of the text variable’s contents. If all is correct, you should see a trace with the txt file’s contents.
EmbedTXTTest

Using Flex SWFs as Component UI or in J2EE Tapestry

Flex and Flash swfs have some small differences in how they work. While Flex produces a 2-frame swf, it doesn’t work quite right in certain situations. If you’ve got a project where Flash or an AS3 project in Flex/FlashBuilder produces SWFs that work just fine and Flex/Flashbuilder MXML-based swfs don’t (if you can right click your partially loaded MXML-based swf and say play and it works), this will probably solve your problem.

Anyone building Custom UI components in Flash might be thinking about using Flex, which, until today would prove difficult. I’ve got some other tutorials out there on how to use Flex UIs in Flash, but they were missing a big part–embedding capability.

Now that we can bypass the preloader in flex, we can use this same process to kick-start our Flex files when using them in Flash or in Tapestry (I assume Tapestry works, please let me know if it doesn’t).

Bypassing the Flex Preloader

Ahh, one of those holy grail type posts. After days of searching to answer a problem, I’ve managed to solve it by completely bypassing the preloader in Flex. It can be done, and here’s how:

Flex uses a 2-frame configuration to display its content. The Preloader is put on the first frame, and is downloaded immediately. It then waits for the rest of the content to load and fires its complete event. Then the SystemManager jumps to frame 2 and everything’s ready to go. To turn this off, we need to do two things:

1: Create a preloader shim file named PreloaderShim.as:

package
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
 
	import mx.managers.SystemManager;
	import mx.preloaders.DownloadProgressBar;
 
	public class PreloaderShim extends DownloadProgressBar
	{
		private var delay:Timer = new Timer(1);
 
		override public function set preloader(value:Sprite):void
		{
			SystemManager(value.parent).gotoAndStop(2);
			delay.addEventListener(TimerEvent.TIMER, go);
			delay.start();
		}
 
		public function go(e:TimerEvent):void{
			delay.stop();
			delay.removeEventListener(TimerEvent.TIMER, go);
			delay = null;
			dispatchEvent(new Event(Event.COMPLETE));
		}
 
	}
}

2: Use this in our Flex file:

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
				preloader="PreloaderShim">

We override the set preloader function of the DownloadProgressBar class. This is automatically set to the preloader instance in Flex. This instance’s parent is the SystemManager. The SystemManager has a gotoAndStop method that allows us to skip to the next frame. Almost immediately, we can fire the complete event. I think that Flex needs a single millisecond to begin its download process, so we wait that long before firing the complete. After that, Flex loads, no preloader perceived.

The Flex part is easy–just point your Flex project to have this new class as a preloader. Just drop your PreloaderShim.as file in with your mxml files and you’re off and running!

Note, I’ve only tried this on local files, and have NO idea how it would work in a real, downloading production file. It works fine for me, but please test it before you go breaking things.

clAir – alpha – a Craigslist aggregator in Adobe Air

I’ve been working on a little side project for a while to be able to view multiple Craigslist results pages at once. To make this happen, I’ve built a basic app in PHP, but I thought it would be significantly nicer to build something standalone and FAST. I tried Flex, but the security sandbox was a pain, as was a lack of useful features, so here we go! Please leave feedback and bugs, if you find them.
clair1

Adding an AS3 Sprite to an MXML file

Recently, working on an FLV component, I tried to add a sprite to an MXML component. Since UIComponent inherits Sprite, it’s too high up to be added to the stage. What you CAN do, though, is add the sprite to a UIComponent component in MXML and display it that way.

I’ve used the As3FLV component I wrote in this example. the As3FLV extends sprite, so all we have to do is add it to the UIComponent I’ve created in MXML and it should show up. I’ve also built the As3FLV component to resize itself based on the parent size etc.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
				layout="absolute"
				xmlns:local="*"
				xmlns:net="flash.net.*"
				width="100%"
				height="100%"
				creationComplete="init();">
	<mx:Script>
		<![CDATA[
			import com.MEMD.As3FLV;
			private var flv:As3FLV = new As3FLV();
 
			public function init():void
			{
				flv.source = "http://www.mediacollege.com/video-gallery/testclips/20051210-w50s.flv";
				holder.addChild(flv);
			}
		]]>
	</mx:Script>
	<mx:UIComponent id="holder"
					width="100%"
					height="100%"/>
</mx:Application>

From the top: creationComplete=”init();” – runs the init function at the beginning
import com.MEMD.As3FLV; – imports this class
private var flv:As3FLV = new As3FLV(); – creates a variable named “flv” as an instance of As3FLV
flv.source = “http://www.mediacollege.com/video-gallery/testclips/20051210-w50s.flv”; – sets the source of “flv” to a sample FLV
holder.addChild(flv); – adds our instance to the UIComponent below

FlexFLV Component

In working with the VideoDisplay component, I ran into occasional problems. Things like setting autoplay to false killing my metadata and such. This is the first draft of a replacement. It doesn’t have any of the crazy bells and whistles of the FLVPlayback component — skinning etc., but it does have most every feature I could think of putting in. If you’re not working in Flex and want the AS3 version, check it out here. If you want to use the AS3 version but use it in Flex, check out Adding an AS3 Sprite to an MXML file