Objeto de sonido reciclables en ActionScript 3?
-
13-09-2019 - |
Pregunta
El uso de un sonido () objeto en ActionScript3, ¿cómo puedo jugar una MP3 y luego, cuando el usuario elige una diferente, jugar un segundo sonido, usando el mismo sonido () del objeto?
EDIT: Véase mi respuesta a cómo lo hice
.Solución
No se puede utilizar un mismo objeto Sound
para jugar varios archivos .
Una vez
load()
se llama en un objetoSound
, no se puede cargar posteriormente un archivo de sonido distinto en el objeto de sonido. Para cargar un archivo de sonido diferente, cree un nuevo objetoSound
.
Otros consejos
Ok, de hecho me hice usando el siguiente código. Mi error fue en otro lugar en el archivo FLA, pero esto funciona. Hice una variable global no inicializado y creado el objeto Sound () LOCAL dentro de una función. Mientras estoy técnicamente el uso de múltiples objetos sonoros, mis referencias están apuntando a un objeto. Además, puedo llamar a estos métodos una sobre otra para la codificación más fácil. Esto funciona para mí:
/* -------------
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