2009-12-06 84 views
0

在Actionscript3中使用ONE Sound()對象時,如何使用SAME Sound()對象來播放一個MP3,然後在用戶選擇另一個MP3時播放第二個聲音?Actionscript 3中的可循環聲音對象?

編輯:看我的答案是如何做到的。

回答

0

您不能使用相同的Sound對象玩multiple files

一旦load()Sound對象上被調用,您不能以後將不同的聲音文件加載到該聲音對象。要加載不同的聲音文件,請創建一個新的Sound對象。

+0

其實,我解決了我的問題。從技術上講,你是對的,但是,我用一個聲音對象來播放多個文件。我會在幾分鐘後在這裏發佈代碼,告訴你如何。 – Moshe 2009-12-06 13:13:42

0

好吧,我其實使用下面的代碼。我的錯誤在FLA文件中的其他地方,但是這是有效的。我做了一個未初始化的全局變量,並在函數內部創建了LOCALLY聲音()對象。雖然我在技術上使用多個聲音對象,但我的參考都指向一個對象。另外,我可以通過調用這些方法來簡化編碼。這適用於我:

/* ------------- 

Sound player 
functions 

------------ */ 

var snd:Sound;      //the sound object 
var sndC:SoundChannel;    //the soudchannel used as "controller" 
var sndT:SoundTransform;   //soundTransform used for volume 
var vol:Number = 1;     //the volume of the song 
var pan:Number = 0;     //panning of the sound 
var pos:Number = 0;     //position of the song 
var currentSound:String;    //currently playing song? 


function playSound(s:String){         //this function resets the sound and plays it 
    stopSound(sndC);           //stop the sound from playing 
    snd = new Sound();           //reset the sound 
    snd.load(new URLRequest(s));        //load the desired sound  
    sndC = new SoundChannel();         //(re-)apply the sound channel 
    applyVolume(vol,pan,sndT,sndC);        //apply the volume 
    sndC = snd.play(pos);          //play it 
    sndC.addEventListener(Event.SOUND_COMPLETE, startSound); //remind it to restart playing when it's done 
}                //end function 

function applyVolume(n:Number, p:Number, st:SoundTransform, sc:SoundChannel){ //takes an argument for the volume, pan, soundTYransform and soundChannel 
    sndT = new SoundTransform(n,p);            //applies the soundTransfrom settings 
    sndC.soundTransform = sndT;             //and attaches it to the soundChannel 
}                    //end function 

function stopSound(sndC:SoundChannel){   //this function stops a sound from playing 
    if(sndC != null){       //if the sound was used before (ie: playing) 
     if(currentLabel == "video-frame"){  //if we are in the video frame 
      pos = sndC.position;     //store the position of the song to play from at a later time 
     }else{         //otherwise 
      pos = 0;        //set the position at 0 
     }          //end if 
     sndC.stop();       //stop it 
    }           //end if 
}            //end function 

function startSound(snd:Sound){     //restarts a sound when it's playing 
    if(snd != null){       //if the sound exists 
     sndC = snd.play(pos);     //play it 
    }           //end if 
}            //end function