How do i play a song from library when the preloader is a “x”% of total loaded?

假装没事ソ 提交于 2020-01-16 18:59:12

问题


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);
    }
}

回答1:


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



来源:https://stackoverflow.com/questions/21612577/how-do-i-play-a-song-from-library-when-the-preloader-is-a-x-of-total-loaded

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!