Quiero reproducir canciones mps de fondo en mi aplicación Flex.
Las url´s me vienen de BD y las tengo en un array de clases CancionMp3 dentro del campo urlCancionMp3.
Tengo 2 botones Play y Stop que me funcionan a las mil maravillas.
El problema viene cuando intento reproducir la siguiente canción.
Lo que hago es que mi channel esuche mediante el evento Event.SOUND_COMPLETE hasta que la canción acabe, y esto cuando ocurre pretendo crear otro Sound cargando una nueva urlRequest con la nueva canción, asignárselo al channel y que la reproduzca. Este es mi código:
<?xml version="1.0" encoding="utf-8"?>
<mx:HBox xmlns:mx="http://www.adobe.com/2006/mxml" verticalAlign="bottom" horizontalAlign="right" horizontalGap="3">
<mx:Script>
<![CDATA[
import com.cc.to.model.CHModelLocator;
import com.cc.to.util.Traductor;
import com.cc.tovo.CancionMp3VO;
import mx.messaging.Channel;
private var sound : Sound;
private var channel : SoundChannel;
private var _playing : Boolean = false;
private var sonidoDesactivado : Boolean = false;
private var indice : int = 0;
[Bindable]
private var model : CHModelLocator = CHModelLocator.getInstance ();
override protected function createChildren():void {
super.createChildren();
sound = new Sound ();
sound.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
}
private function ioErrorHandler (event : IOErrorEvent) : void {
}
private function cambiarCancion (event : Event): void {
if (sound == null)
{sound = new Sound ();}
indice = indice + 1;
var url : URLRequest = new URLRequest((model.multimedia.canciones.getItemAt(indice) as CancionMp3VO).urlCancionMp3);
sound.load(url);
channel = sound.play();
}
override protected function commitProperties():void {
super.commitProperties();
if (_playing) {
if ((!channel) && (!sonidoDesactivado))
{sound.load (new URLRequest((model.multimedia.canciones.getItemAt(indice) as CancionMp3VO).urlCancionMp3));
channel = sound.play(0,1);
channel.addEventListener(Event.SOUND_COMPLETE,cambiarCancion);}
}
else {
if (channel)
channel.stop();
channel = null;
}
}
public function set playing (valor : Boolean) : void {
valor = false;
_playing = valor;
invalidateProperties();
}
private function activarSonido () : void {
sonidoDesactivado = false;
if (_playing) {
if ((!channel) && (!sonidoDesactivado))
channel = sound.play(0, int.MAX_VALUE);
channel.addEventListener(Event.SOUND_COMPLETE,cambiarCancion);
}
}
private function desactivarSonido () : void {
sonidoDesactivado = true;
if (channel)
channel.stop();
channel = null;
}
]]>
</mx:Script>
<mx:Label text="{Traductor.traducir('cliente.musica', model.mensajes)}"/>
<mx:Image id="imgStop" source="secure/assets/stop.png"
alpha="0.5" mouseOver="imgStop.alpha = 1" mouseOut="imgStop.alpha=0.5"
width="16" height="16" click="desactivarSonido()"/>
<mx:Image id="imgPlay" source="secure/assets/play.png"
alpha="0.5" mouseOver="imgPlay.alpha = 1" mouseOut="imgPlay.alpha=0.5"
width="16" height="16"
click="activarSonido()"/>
</mx:HBox>
El problema viene cuando entra en el CambiarCancion, al hacer el soun.load(url) me dice:
Main Thread (Suspended: Error: Error #2037: Se llamó a las funciones en una secuencia incorrecta o la llamada anterior no se produjo correctamente.)
flash.media::Sound/_load [no source]
flash.media::Sound/load [no source]
com.cc.to.components::PGAudio/cambiarCancion
Alguna luz sobre el tema?, gracias de antemano.
Saludos.
