2009-12-06 4 views
0

En utilisant un seul objet Sound() dans Actionscript3, comment puis-je lire un fichier MP3 puis, lorsque l'utilisateur en choisit un autre, lire un son second, en utilisant l'objet SAME Sound()?Objet sonore recyclable dans Actionscript 3?

EDIT: Voir ma réponse pour comment je l'ai fait.

Répondre

0

Vous ne pouvez pas utiliser le même objet Sound pour jouer multiple files.

Une fois load() est appelée sur un objet Sound, vous ne pouvez plus charger un fichier audio dans l'objet Sound. Pour charger un fichier son différent, créez un nouvel objet Sound.

+0

En fait, j'ai résolu mon problème. Techniquement vous avez raison, mais oui, j'ai utilisé un objet sonore pour lire plusieurs fichiers. Je vais poster le code ici dans quelques minutes pour vous montrer comment. – Moshe

0

Ok, je l'ai fait en utilisant le code suivant. Mon bug était ailleurs dans le fichier FLA, mais cela fonctionne. J'ai créé une variable globale non initialisée et créé l'objet Sound() LOCALEMENT à l'intérieur d'une fonction. Bien que j'utilise techniquement plusieurs objets sonores, mes références pointent toutes vers un seul objet. De plus, je peux appeler ces méthodes les unes sur les autres pour faciliter le codage. Cela fonctionne pour moi:

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

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