2009 AS3: Partition Slider Source

Back in 2009 I started to code up an AS3 partition (range) slider, that is, a slider with multiple thumbs. Three in fact. It’s not dynamic by any means.

There is a Document Class but it’s tied directly to assets on the Stage. So I am releasing this just in case someone might need one and has the patience or desire to re-code it to actually be dynamic and allow for a different number of thumbs, etc.

There is some commenting in the Document Class about the thing – what it does and does not do.  You could create a dynamic thing in an hour or two to suit your needs by writing a few more Classes – one for the control itself, one for thumb controls, another for a thumb dragging manager, and maybe some custom events, etc. Please don’t judge me by the source 🙂

Download Link (zipped files): 44kb.

Tagged : / /

jQuery.post() for AS3

I don’t partake of the Adobe Flash AS3 code bubbler very much any more, having saddled up with Objective-C and iOS/OS X development a lot more these days.

However, I do dabble in AS3 from time to time, generating some prototypes and simulations with it still. AS3 has been around for quite a number of years now and it does have it’s good points. It’s usually quite quick to roll something up quickly in regards to some kind of data visualization, audio/video manipulation, etc.

I just found (it’s already over three years old) a neat Class that simplifies AS3’s URLRequest class… making the loading of images, SWFs, sounds, downloading files, uploading files, and making web service calls a snap.

Head on over to http://code.google.com/p/quiero/ and check it out. Look what it can do below just for an idea.

Go from this

var request:URLRequest = new URLRequest('http://google.com/ig/api');
var loader:URLLoader = new URLLoader();
var variables:URLVariables = new URLVariables();
variables.weather = 'Salem, OR';
request.method = URLRequestMethod.GET;
loader.addEventListener(Event.COMPLETE,onRequestComplete);
loader.load(request);

function onRequestComplete(e:Event):void
{
  trace(e.target.data)
}

To this

import quiero.*
Quiero.request({url:'http://google.com/ig/api',method:'get',data:{weather:'Salem, OR'},onComplete:onRequestComplete})

function onRequestComplete(e:RequesterEvent):void
{
  trace(e.data)
}

Update: Google shut the door on this query, I tried out Yahoo! and it works fine, you just need to deal with a namespace.

Tagged : /

ActionScript 3: Multiple MP3s with visualizer

The past day or two I’ve spent trying to figure out how to get multiple, different MP3s to display some visualization for Sprites that represent them. computeSpectrum was obviously out of the question. I then turned to Sound.extract() to see if that could help me.

Not knowing all the intricacies of that method (I haven’t touched Flash in about five years – no lie), I beat my head against a wall until I discovered that it’s meant for dynamic audio (creation)… the sample data event wasn’t firing for me – because I had loaded an MP3 into each sound. Hmm.

I was overlooking something quite simple. SoundChannel has left and right peak values. I could poll those with a Timer, normalize for panning, and apply those values to a Sprite with scaling – to generally pulse the thing to show activity. I didn’t really need to get the spectrum data because I wasn’t going to use it that way.

So sometimes it’s the easy route that gets overlooked for a more complicated, more satisfying solution. Granted, I’m still happy – but wouldn’t it be nice if somehow computeSpectrum was added somehow to SoundChannel?

Xcode gotcha: all project files compile

A while ago I forked my development endeavors my tackling iPhone development. Coming to it from an AS3 background made some parts easier than others to assimilate. One of the things in AS3 is that you could have packages of classes, etc. in a project, but if they were not imported or included into class files actually implementing them, they wouldn’t compile into the SWF(s). That appears to not be the case in development for the iPhone/Mac.

Recently I was debugging a project on 1st generation Touches. And everything was working perfectly. I then began debugging and developing on a 2nd generation Touch. This had a higher version of iOS because it could actually run it. 1st generation Touches have fallen back because they don’t have enough horses under the hood to properly power later advances in iOS.

I began playing with a screen mirroring class that I planned on using to show my application’s UI on a big panel using an iPad dock connector to VGA adapter. So I wrote up the class and there it sat in my Project. I implemented the class in my app delegate.

Later I decided that I wouldn’t need it. So I removed the class import statement in my app delegate’s .m file, and I removed the setup method from the didFinishLaunchingWithOptions method. I thought nothing of it and carried on thinking that class would now be free from future compiles.

I compiled my application back onto a test 1st generation Touch (which doesn’t support UIScreenDidConnectNotification which is used in the screen mirroring class), and my application would crash with a

dyld: Symbol not found: _UIScreenDidConnectNotification
Referenced from: …/AppName.app/AppName
Expected in: /System/Library/Frameworks/UIKit.framework/UIKit

Hmmm. I removed the import and the method implementation. Did I need to clean the project because something was being cached? Tried that to no avail. Something was decidedly wrong. I googled the error and found a hit or two mentioning weak linking, etc. Tried that and obtained the same crashing results.

A quick jaunt over to the Apple Developer Forums was needed and I posted my problem there. After a bit of time I received my answer… category implementation. The fact that the class is in my project at all means it’s getting linked.

But what is a category? It’s an alternative to subclassing. You add methods to a class (remember the prototype days in AS2?) Any methods that are added to a class through a category become part of the class definition. Meaning ANY instance or subclass of the class will have access to the added methods. Cool, I hadn’t seen or knowingly run into this before. It’s great to know, but  was kicking me squarely in the balls because of my naivete. Again, good to know and can be quite useful!

I pulled the class guts of the screen mirroring class from an open-source class online. And guess what? It was using a category for UIApplication.

@interface UIApplication (ScreenMirroring)

But still, the fact that I wasn’t importing the class at all made me assume that it wouldn’t be compiled into the Target. What I was told on the forum:

The source code that makes the offending reference to UIScreenDidConnectNotification is still being compiled because it’s in a file that’s part of your project.

Well… does this mean if you write a bunch of throw-away classes in your project that you’re not using, they still get compiled into your Target(s)? It would appear so and is a break from the mindset I’ve had in project development for a long time, coming from a heavy AS3 background. I would consider this a gotcha. Perhaps something that those with a lot more Xcode experience would scoff at, but you could run into this situation as well. The fact that all the code in your project compiles is something I wasn’t aware of. Trim one’s projects.

Tagged : /

Adobe Air and included files

Have you as a developer ever wanted to deploy an Adobe Air application and include files with the installation? Say an XML file for settings and the like? Well, you can include files well enough (to the applicationDirectory), but if you want to write back to that file you’ll get an error. You see, files in the applicationDirectory are read-only.

So what do you do? You’ll need to read that file in the applicationDirectory and then copy it over to the applicationStorageDirectory… or desktopDirectory or even the documentsDirectory. Then you’ll be able to read and write to the file.

The snippet below sets up a reference to a test text document I included in an AIR application. It exists in the protected applicationDirectory. Then the destination (of applicationStorageDirectory) is referenced. If the file doesn’t already exist in the destination location, the one in the applicationDirectory is copied over to the applicationStorageDirectory… and traces “Copy Done” for me.

If you ever wanted to reset the settings file, all you would need to do is to copy over that original included Test.txt file to your applicationStorageDirectory again, over-writing the one that was already there. I didn’t see anything like this in my Google searches, but I did hear of the technique. Here is a little code that shows it in action.

var testFile:File = File.applicationDirectory.resolvePath("Test.txt");
			
//Copy the file over
var destination:File = File.applicationStorageDirectory;
destination = destination.resolvePath("Test.txt");
//Only copy the file from the installation bundle if it doesn't exist in the applicationStorageDirectory
if( !destination.exists ){
	if( testFile.copyTo(destination,true)){
		trace("Copy done");
	}
}

One thing to note – on a Mac, the applicationStorageDirectory exists here: username/Library/Preferences//Local Store/

Of course it exists elsewhere on a PC and Linux, but the applicationStorageDirectory reference takes care of that for you on the different operating systems. You should check your Local Store just to make sure it’s doing what you need it to do.

Software Voice Vowel Detection in ActionScript 3.0

Lest you think that I have come up with the solution to this and you merely look for a download link, I have to let you know that I’ve come pretty close but gave up. I’ll tell you what I did and why I decided to put the project down. If you’ve followed the thread on Flashcoders, you already have some insight. Perhaps this post might get you thinking and you may come up with a workable solution!

Start. While implementing a text to speech engine (which returns an on-the-fly .mp3 file), I harnessed the power of SoundMixer.computeSpectrum. This allowed me to pretty easily move the jaw on a character up and down based upon the amplitude of the audio playback. When not moving the jaw too drastically, it looks pretty decent.

But what I really wanted to do was to shape the mouth to match the audio as best I could. Since I was using a software voice (not related in any way to Mac OS X’s voices), I could more accurately theoretically match patterns.

I began by creating a spectrum analyzer so I could evaluate the .readFloat values coming through SoundMixer. Now, I wanted to generate vowel “patterns” of values that I could store and use for matching later on the fly. I added an input text field and a speak button. A handy array in my spectrum class would gobble up values as they poured through the SoundMixer. Another button would later trace out all of the values captured. Yes, I just ran this application for each vowel I entered and played back. I ignored all zero values for each vowel, as there were tons of these… mostly at the beginning and the end of the audio file playing back.

Continue reading “Software Voice Vowel Detection in ActionScript 3.0”

I have discovered flash.net.ServerSocket and it’s sticky sweet.

I have just discovered the ServerSocket class and I have made an AIR2 (required) server socket application with it. I’ve tested it locally so far (127.0.0.1, 0.0.0.0 ) and it seems to be working fine with a regular application I made using Socket to talk with it. I tried getting another copy of the socket app to talk to the socket server from another machine on the network, but I think a LAN might work better for testing at the moment since I don’t know the dynamics of the huge network that exists at work.

Continue reading “I have discovered flash.net.ServerSocket and it’s sticky sweet.”

AS3, AIR, calling document class methods from a spawned window

I am creating an AIR application that spawns a new window, so there is a main application window and also another window. One can manipulate data in either window, and data is synchronized between the windows. So when I adjust data in the main window, I update the UI and data that’s in the secondary window.

The main window is a document class, and a class represents that which sits in the secondary window. I have updating methods in the secondary class so it’s easy to call them from the document class and have the magic happen.

However, if I change things in my secondary window, I update the UI there and the local copy of the underlying data, and need to tell the document class to do the same. This should be using a database or something so there is only ever one copy of the data both consume, but I was feeling lazy.

Anyway, after pounding my head for 30 minutes on how to access the main window and it’s document class from the class sitting in the spawned window of the AIR application, I finally figured it out.

import flash.desktop.*;
var who = NativeApplication.nativeApplication.openedWindows[0].stage.getChildAt(0);
who.callTheMethodInDocumentClass();

And that does it. You could also just do the following too:

NativeApplication.nativeApplication.openedWindows[0].stage.
getChildAt(0).callTheMethodInDocumentClass();
You can call methods in your main document class from a spawned window in an AIR application, send along whatever parameters you’d like, etc. This is a big time saver now that I figured this out. If this is common knowledge, I didn’t know about it.

Anyway, enjoy.