Hello,
I’ve been trying to search about my problem but can’t found the answer.
My problem is: I’m trying to create an external class which function is load an external sound file and play or stop depends on stage.activate and stage.deactivate events.
The actionscript 3 in the timeline is functioning properly, but when I try to create as external class, it has so many errors.
So many that I can’t troubleshoot them. Btw, it’s my first class creation.
Is there anybody who kindly enough to give me some guidance to create a proper class with stage event listener?
here’s my as3:
import flash.events.Event;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.net.URLRequest;
import flash.filesystem.File;
stage.addEventListener(Event.ACTIVATE, flashActive);
stage.addEventListener(Event.DEACTIVATE, flashDeactive);
var sound:Sound = new Sound();
var soundChannel:SoundChannel;
var bgSoundTransform = new SoundTransform();
var lastPosition:Number = 0;
var soundIsPlay:Boolean = false;
var soundLocation:File = File.applicationDirectory;
soundLocation = soundLocation.resolvePath("media"+File.separator+"backsound_loop.mp3");
sound.load(new URLRequest(soundLocation.url));
soundChannel = sound.play();
soundIsPlay = true;
sound.addEventListener(Event.COMPLETE, onSoundLoadComplete);
function soundPlayer():void
{
if (! soundIsPlay)
{
soundChannel = sound.play(lastPosition);
soundIsPlay = true;
}
else
{
trace("Sound Already Played");
}
}
function soundStopper():void
{
if (soundIsPlay)
{
lastPosition = soundChannel.position;
soundChannel.stop();
soundIsPlay = false;
}
else
{
trace("Sound Already Stopped");
}
}
function onSoundLoadComplete(e:Event):void
{
sound.removeEventListener(Event.COMPLETE, onSoundLoadComplete);
soundChannel = sound.play();
soundChannel.addEventListener(Event.SOUND_COMPLETE, onSoundChannelSoundComplete);
}
function onSoundChannelSoundComplete(e:Event):void
{
e.currentTarget.removeEventListener(Event.SOUND_COMPLETE, onSoundChannelSoundComplete);
soundChannel = sound.play();
}
function flashActive(event:Event):void
{
stage.frameRate = 24;
soundPlayer();
trace("flash gained focus, now frame rate is "+ stage.frameRate);
}
function flashDeactive(event:Event):void
{
stage.frameRate = 0;
soundStopper();
trace("flash lost focus, now frame rate is "+ stage.frameRate);
}
The above timeline script is loading file backsound_loop.mp3 from media folder in application directory.
I’m trying to create a class, so I can call for example: createBackSoundLoop(sound_file)
yes, i get the above code from various sources and it works from timeline.
any guidances?
thanks