Actionscript

Calling a JavaScript function from Actionscript 3 (Flash)

It’s very easy:

import flash.external.ExternalInterface;
...
ExternalInterface.call("your_javascript_function()");

You can even get a return value:

var x:int = ExternalInterface.call("get_x()");

Archived under Actionscript, Flash, JavaScript, Web Development Comments

Flash CS3 is slow on Windows Vista

Here is how to fix it:
1 - Right click on Flash’s desktop (or whatever) icon and then click “Properties”
2 - Go to compatibility tab
3 - Check the box “Run this program in compatibility mode for:”
4 - From the drop down select “Windows XP (Service Pack 2)”
5 - Open Flash

And that’s it, it will run smoothly, or at least it did for me.

Archived under Actionscript, Annoying Stuff, Flash Comments

Red5 flash server problems; impossible to get it working

I worked 2 full days to get Red5 0.7.0 working with custom applications but I had no luck.

I couldn’t even get the tutorials written by Red5 developers working, it doesn’t have proper documentation either so it was a real nightmare.
So many other people have these problems too and there seem to be no answer to these problems.

So here is what I did, I installed Red5 0.6.2 and voila! it worked, all the tutorials worked and all the examples worked too!

I’m not a Java guru and I’m glad I’m not, it looks really huge and complicated, it doesn’t look fun to program in at all. I’m sure so many people like it very much but I really think even C is nicer than Java.
For example, to make an even “Hello World” Red5 application you have to make ~5 folders, 4 XML files, 1 Java class, compile your class, upload it to your server and restart Red5!!!

Update:
Funny, I kind of started liking Java :)

Archived under Actionscript, Annoying Stuff, Flash Comments

NetStream.publish Actionscript 3, recording with the best quality

After some time of working on this I found that with the following code, I could get the best quality when recording videos with Flash:

camera.setMode(300, 240, 30);
// camera.setKeyFrameInterval(CAMERA_KEY_FRAME_INTERVAL); /* Comment this out */
camera.setQuality(0, 0);

Funny, with 15 frames per second, there was a lot of problems, but 30 frames per second fixed all the issues for me.

Please do let me know if you have a better solution :)

Archived under Actionscript, Flash Comments (2)

Detecting the end of FLV stream in Actionscript 3, part 2

Read this post first:
http://blog.code-head.com/detecting-the-end-of-flv-stream-in-actionscript-3

Funny today, I get the events in reverse order:

NetStream.Buffer.Empty
NetStream.Buffer.Flush

Yesterday it was:

NetStream.Buffer.Flush
NetStream.Buffer.Empty

I will find the solution though ;)

Archived under Actionscript, Annoying Stuff, Flash Comments (2)

Detecting the end of FLV stream in Actionscript 3

There is an issue with this post, read it and then read this:
http://blog.code-head.com/detecting-the-end-of-flv-stream-in-actionscript-3-part-2

This is also one of those things that I couldn’t really find a real solution to online, so with some tests and experiments, I came up with this solution which I’m not sure if it’s the best solution.

First you will need to attach an event to your incoming stream, assuming your stream is clientStream:

/* ... */
clientStream = new NetStream(serverConnection);
clientStream.addEventListener(NetStatusEvent.NET_STATUS, PlayerStatusHandler);

Then you will need a member variable or a variable in the global scope to keep the latest status:

var playerLastEvent:String = new String();

Now the PlayerStatusHandler which again, I don’t think it’s very pretty:

function PlayerStatusHandler(event:NetStatusEvent) {
	if (playerLastEvent == "NetStream.Buffer.Flush" && 
		event.info.code == "NetStream.Buffer.Empty") { /* Stopped */
		StopPlayback(event); /* Or any other thing you want to do */
	} else if (playerLastEvent == "NetStream.Buffer.Flush" &&
			   event.info.code != "NetStream.Buffer.Empty")
		playerLastEvent = ""; /* Sometimes it throws the Flush event */
	else if (event.info.code == "NetStream.Buffer.Flush")
		playerLastEvent = "NetStream.Buffer.Flush";
}

So as you can see, we are looking for the Flush event that is followed by an Empty event.

This is just tested on my connection and I don’t really know how this works on slow connections, where the buffer is empty often but maybe you can tell me :)

I wish there was an event to show the real end of the stream.

Archived under Actionscript, Flash Comments (3)

How to show a microphone activity indicator in Actionscript 3

Here is what I did, first setup a timer:

var micDelay:uint	= 100;
var micRepeat:uint	= 0; /* Run forever */
var micTimer:Timer = new Timer(micDelay, micRepeat);

And then tell it to call our function every 100 milliseconds:

micTimer.addEventListener(TimerEvent.TIMER, ShowMicActivity);

Then the function:

var micActivityIndicator:Shape = new Shape();
function ShowMicActivity(e:TimerEvent):void {
	if (mic.activityLevel > 0) {
		var h:int = mic.activityLevel;
		var y:int = 150 - mic.activityLevel;
	} else {
		var h:int = 5;
		var y:int = 150 - 5;
	}
	micActivityIndicator.graphics.clear();
	micActivityIndicator.graphics.beginFill(0x000000);
	micActivityIndicator.graphics.drawRect(0, y, 10, h);
	micActivityIndicator.graphics.endFill();
}

I hope this helps someone, I couldn’t find anything online.

Archived under Actionscript, Flash Comments

Cursor hand when mouse over images, Actionscript 3-Flash

Funny, I couldn’t find the answer to this anywhere, either the answers are unrelated or they are overly complicated. So here is what I did:

         var imageLoader:Loader     = new Loader();
	imageLoader.load(new URLRequest("path to your image/some image.png"));
	var button:Sprite    = new Sprite();
	button.addChild(imageLoader);
	button.buttonMode    = true;
	button.useHandCursor = true;
	addChild(button);

THAT’S IT!!!

Archived under Actionscript, Flash Comments