Actionscript 3中的可回收声音对象?

时间:2009-12-06 05:21:53

标签: actionscript-3 action flash-cs3

在Actionscript3中使用ONE Sound()对象,如何播放一个MP3,然后当用户选择另一个MP3时,使用SAME Sound()对象播放第二个声音?

编辑:请参阅我的答案,了解我是如何做到的。

2 个答案:

答案 0 :(得分:0)

您无法使用相同的Sound对象来播放multiple files

  

load()对象上调用Sound后,您无法在以后将其他声音文件加载到该Sound对象中。要加载其他声音文件,请创建一个新的Sound对象。

答案 1 :(得分:0)

好的,我实际上是使用以下代码完成的。我的错误是在FLA文件中的其他位置,但这是有效的。我创建了一个未初始化的全局变量,并在函数内部创建了Sound()对象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