Jun.24.2008

Actionscript

On with the flash player adventure…

I finally got a rudimentary version of the flash player up and running. Rudimentary, because the one I used to have, was slightly aesthetically better. It got ’swallowed’ by my dead 40G HDD. This one did the old job, with some blemishes.

The original source (with many frills and laces), was taken from ActionScript, which I hacked off everything I don’t need.

And then I realised that the script used to run the stuff behind the scenes, were already outdated. It had plenty of codes, graphics in many layers, and a few files embedded here and there. On the other hand, the new Flash CS3 allows mp3 files to be imported in higher bitrates (unlike the old limit of 160kbps) and in VBR. I can kiss goodbye to conversion from now on.

And so, the journey to upgrade everything (code, filespace used), while still keeping it the way I wanted it to work. In any case, since I’m feeling unwell today, I decided to kill 2 birds with one stone.

First up, was Actionscript.. to rewrite the codes in 3.0 using the new syntax available. And I went searching all over the web for an understanding on how it’s done.

As I looked around, I was more and more amazed. Not only can I call up the embedded file (imported into the library) with tidied codes, there’s also the option of calling a remote file.

And nothing better than what I saw from Tim Brimelow at TheFlashBlog, where he gave an online tutorial at gotoAndLearn() on how it’s done, with many other ideas possible.

From there, the ‘climbing’ continued. To incorporate buttons into the file, and make it work. I tried digesting the instructions from this place, but the code and preparations were too tedious. And there’s too many keyframes for me to put everything in the right places.

Along came this great tutorial from School of Flash, which teaches you how to prepare the code for play/stop/pause/mute, complete with volume slider as well. I didn’t want a huge player on my site, and again, I went hacking away at the unwanted bits. Merged it nicely with the ones from the previous sites for a better understanding, and came up with a working file, which all the 3 buttons will perform the way it should.

The next thing, to have the play and pause buttons to show up only when they are due for clicking, and hide when they are not needed. And I moved elsewhere to find the codes for it, before bumping into this site. 80% of the problem solved.

Then a couple of problems still remain. While I no longer have to open up the flash source to embed a new mp3, I couldn’t get it to run at a lower bitrate. Despite all the ways and means, it’d run the mp3 file as it is. As such it was back to the earlier stages, of embedding the file. Except, I no longer have to place the mp3 file on the stage, and have to change the number of frames. Hopefully someday there’s a code that will convert the file to a lower bitrate, on the fly.

Then the next problem: to loop the song. Until an hour back I’m still not sure why it’d only run once or twice. Again I went around reading on codes.. And found I wasn’t alone. Then I came across a simpler workaround using functions call, better than the former suggestion.

While it is workable, it meant I have to rewrite the structure of the code to fit it all nicely in. And it took another half an hour or so, debugging and testing.

And the final product is ready. While the older one took about 700kb running on 48kbps Mono, this one took about 230kb, going on 128kbps Stereo.

The final code, in case anyone needs a clearer picture.

var song:mp3 = new mp3;
var sc:SoundChannel;
var isPlaying:Boolean;
var pos:Number=0;

playBTN.addEventListener(MouseEvent.CLICK, playButton);
pauseBTN.addEventListener(MouseEvent.CLICK, pauseButton);
rewBTN.addEventListener(MouseEvent.CLICK, rewindButton);

playSong();

function playSong():void{
sc = song.play();
playState();
}

function playState():void {
isPlaying = true;
playBTN.visible = false;
sc.addEventListener(Event.SOUND_COMPLETE, onComplete);
}

function onComplete(event:Event):void {
if (sc != null) {
sc.removeEventListener(Event.SOUND_COMPLETE, onComplete);
playSong();
}
}

function playButton(event:MouseEvent):void {
if (!isPlaying) {
sc = song.play(pos);
playState();
}
}

function pauseButton(event:MouseEvent):void {
if (isPlaying) {
pos = sc.position;
sc.stop();
isPlaying = false;
playBTN.visible = true;
}
}

function rewindButton(event:MouseEvent):void {
if (isPlaying) {
sc.stop();
playSong();
} else {
pos = 0;
isPlaying = false;
}
}

Leave a Reply





Comments for this post will be closed on 22 October 2008.