Question

I am currently undertaking a final exam based on the presentation of an offline portfolio and I need to make a preloader, where when the process of loading is 25% of total bytes, to play a sound that is imported into the library. I've tried several ways and can’t do it.

I'll leave you the code for my preloader.

//(also this code is a mouse loader in text form)

this.loaderInfo.addEventListener(ProgressEvent.PROGRESS,checkingProgress);

function checkingProgress(event:ProgressEvent):void{
    var procentLoaded:Number = event.bytesLoaded/event.bytesTotal*100;

    ptxt.text=int(procentLoaded)+"%";
    if(procentLoaded == 100){
        this.gotoAndPlay(1,"Video-Int");
    }
}

stage.addChild(ptxt);
ptxt.mouseEnabled = false;
ptxt.addEventListener(Event.ENTER_FRAME, fl_CustomMouseCursor);

function fl_CustomMouseCursor(event:Event) {
    ptxt.x = stage.mouseX;
    ptxt.y = stage.mouseY;
}
Mouse.hide();

this.addEventListener(Event.ENTER_FRAME, loading);

function loading(e:Event):void {
    var total:Number = this.stage.loaderInfo.bytesTotal;
    var loaded:Number = this.stage.loaderInfo.bytesLoaded;

    prel.scaleX = loaded/total;
    if (total == loaded){
        play();
        this.removeEventListener(Event.ENTER_FRAME, loading);
    }
}
Was it helpful?

Solution

You need to make the sound file in your library available for actionscript.
Give it a Class name ex. MySound

...
var mysound:MySound = new MySound();
var soundPLaying:Boolean = false;

function checkingProgress(event:ProgressEvent):void{
var procentLoaded:Number = event.bytesLoaded/event.bytesTotal*100;

ptxt.text= procentLoaded +"%";
if(procentLoaded > 25 && !soundPlaying){ //check if loaded mor then 25% and sound isn't playing
    mysound.play();
    soundPlaying = true;
}
if(procentLoaded == 100){
    this.gotoAndPlay(1,"Video-Int");
   }
}
...

if you need to do some extra stuff like pause, change volume etc you will need a SoundChannel Object

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top