Anda TODO, incluso muchas de las funciones de AS las manejo desde JS.. como Play.. Pause.. etc..
Pero cuando llega la hora del ExternalInterface pareciera como si "interrumpiera" el streaming :s
Aca dejo el codigo.. no es muy complejo
Código ActionScript :
package Audio
{
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLVariables;
import flash.net.URLRequestMethod;
import flash.net.URLLoaderDataFormat;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundLoaderContext;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.external.ExternalInterface;
public class Player
{
private var _Source:String = '/music/show/songs';
private var _Loader:URLLoader;
private var _Request:URLRequest;
private var Variables:URLVariables;
private var Song:Object = null;
private var Songs:Array = null;
private var sound:Sound = null;
private var Channel:SoundChannel;
private var Buffer:SoundLoaderContext;
private var Position:Number = 0;
private var Loading:Boolean = false;
private var Playing:Boolean = false;
private var Requesting:Boolean = false;
public function Player():void
{
Buffer = new SoundLoaderContext(2*1000);
_Loader = new URLLoader();
_Loader.dataFormat = URLLoaderDataFormat.TEXT;
_Loader.addEventListener(Event.COMPLETE, Response);
Variables = new URLVariables();
}
public function Data(key:String, _value:String):void { Variables[key] = _value; }
public function Source(source:String):void { _Source = source; }
public function Request():void
{
if(Requesting) _Loader.close();
_Request = new URLRequest(_Source);
_Request.method = URLRequestMethod.POST;
_Request.data = Variables;
_Loader.load(_Request);
Requesting = true;
}
function Response(e:Event):void
{
Requesting = false;
var json:JSON = new JSON();
Songs = json.Parse(e.target.data);
Song = null;
}
public function Load(track:uint):void
{
Loading = true;
unLoad();
Song = Songs[track];
sound = new Sound();
addEvents();
sound.load(new URLRequest(Song.url), Buffer);
}
private function unLoad():void
{
if(!sound) return;
Stop();
if(sound.isBuffering)
sound.close();
removeEvents();
}
private function addEvents():void
{
sound.addEventListener(Event.OPEN, Open);
sound.addEventListener(Event.COMPLETE, Complete);
sound.addEventListener(ProgressEvent.PROGRESS, Progress);
sound.addEventListener(IOErrorEvent.IO_ERROR, ioError);
}
private function removeEvents():void
{
sound.removeEventListener(Event.OPEN, Open);
sound.removeEventListener(Event.COMPLETE, Complete);
sound.removeEventListener(ProgressEvent.PROGRESS, Progress);
sound.removeEventListener(IOErrorEvent.IO_ERROR, ioError);
}
private function Open(e:Event):void
{
Loading = false;
Play();
}
public function Play():void
{
if(!sound || Loading || Playing) return;
Playing = true;
Channel = sound.play(Position);
Channel.addEventListener(Event.SOUND_COMPLETE, soundComplete);
}
public function Pause():void
{
if(!sound || Loading || !Playing) return;
Playing = false;
Position = Channel.position;
Channel.stop();
Channel.removeEventListener(Event.SOUND_COMPLETE, soundComplete);
}
public function Stop():void
{
if(!sound) return;
Playing = false;
Position = 0;
Channel.stop();
Channel.removeEventListener(Event.SOUND_COMPLETE, soundComplete);
}
private function Complete(e:Event):void { }
private function Progress(e:Event):void
{
//ExternalInterface.call("Progress", "hola");
}
private function ioError(e:IOErrorEvent):void
{
Loading = false;
Playing = false;
ExternalInterface.call("alert", e.text);
}
private function soundComplete(e:Event):void
{
Playing = false;
Position = 0;
Channel.stop();
Channel.removeEventListener(Event.SOUND_COMPLETE, soundComplete);
}
}
}
saludos y gracias