views:

34

answers:

2

Hey all,

I am a total flash newbie, and I'm looking to build an audio player that uses flash in the back-end, javascript in the front-end. Therefore I do not need the "movie" capabilities of flash. I have the actionscript class that runs the audio, the javascript that tells the actionscript to play, and swfobject to embed the flash. I don't know how to run code once actionscript loads. I've been looking at flex and mxml for possible solutions.. but I'm not sure.. here's what I have.

Audio.as

package {
    import flash.media.*;
    import flash.net.*;

    public class Audio {
        public var file:String;
        public var audio:Sound;
        public var channel:SoundChannel;

        public function Audio(f:String) {
            file = f;
            audio = new Sound();
            audio.load(new URLRequest(file));
        }

        public function play(resumeTime:uint = 0):void {
            if(channel)
                channel.stop();
            channel = audio.play(resumeTime * 1000);
        }

        public function pause():void {
            channel.stop();
        }

        public function progress():Number {
            if(!channel || audio.length == 0) return -1;
            var p:Number = channel.position / 1000;
            var l:Number = audio.length / 1000;
            return p / l;
        }

        public function time():Object {
            if(!channel || audio.length == 0) return {};
            var p:Number = channel.position / 1000;
            var min:int = p / 60;
            var sec:int = p % 60;
            return {"minutes" : min, "seconds" : sec};
        }
    }
}

Javascript:

// Prepare SWF Object
            var flashvars = {};
            flashvars.file = file;
            var params = {};
            params.allowscriptingaccess = "always";
            params.allownetworking = "all";
            var attributes = {};
            attributes.id = "FlashAudio";
            swfobject.embedSWF("Audio.swf", "audio", 1, 1, "9.0.0", false, flashvars,params,attributes);

            swfobject.addLoadEvent(function() {
                AudioCore.audio = swfobject.getObjectById('FlashAudio');
            });

I know relatively how to use ExternalInterface, I just don't know how to run code once the swf loads. Where's the "main" function!?

A: 

You need to have at least one Class in your actionscript project to be a DisplayObjectContatiner (MovieClip or Sprite will do that). And the constructor for that class will behave as Main function for your swf.

bhups
Alright. I would like something as lightweight as possible.. any suggestions?
Matt
+1  A: 

You can compile only Sprites using mxmlc. Include the Audio class in a placeholder Sprite.

package
{
  import flash.display.Sprite;

  [SWF(backgroundColor="0xFFFFFF",width="50", height="50")]
  public class Test extends Sprite
  {
    public var audio:Audio;
    public function Test()
    {
       audio = Audio();
    }
  }
}

By the way, how's the interaction between javascript and Flash done? Check out the ExternalInterface class if you haven't already.

Amarghosh
Thanks a lot! Yah I figured out ExternalInterface, which took forever because even Adobe hosts out of date apis.
Matt