Avg. Rating 3.3

Problem

How can I create an MP3 Player that can begin streaming a song without experiencing any appreciable loading time delay?

Solution

Utilizing a Flash Media Server and Flex 2, you can easily stream the MP3, so it can begin to play immediately. Additionally, the ID3 information can be displayed for those MP3's containing this data.

Detailed explanation

Demo             Source

ID3 Reference

The older version of Flash Media Server (FMS 2.0) does not use the newer AMF3 that Flex 2/3 supports, but fortunately Flex 2/3 can also support the older AMF0 type (or the newer, AMF3 type):

private function initApp():void {
    nc = new NetConnection();
    nc.objectEncoding = flash.net.ObjectEncoding.AMF0; //for FMS
2.0
    //nc.objectEncoding = flash.net.ObjectEncoding.AMF3; //for
newer FMS
}

private function createConnection():void {
    if(!nc.connected) {
        nc.connect("rtmp://your_fms_rtmp_url/your_app_name");
        nc.addEventListener(NetStatusEvent.NET_STATUS,
netStatusHandler); 
    } else {
        nc.close();
        isConnected = false;
    }
}

private function netStatusHandler(event:NetStatusEvent):void {
    switch(event.info.code) {
        case "NetConnection.Connect.Success":
            connectionSuccess(new Event("success"));
            btnPlay.enabled = true;
            break;
        case "NetConnection.Connect.Failed":
            connectionFailed(new Event("failed"));
            break;
        default:
            Alert.show("netStatusHandler:code: " +
event.info.code);
            break;
    }
}

private function connectionSuccess(event:Event):void {
    isConnected = true;
}

private function connectionFailed(event:Event):void {
    isConnected = false;    
}

private function onSongClick(selItm:Object):void {
    frmID3.visible = false;
    frmID3.height = 0;
    mp3Song = selItm.toString();
    //Alert.show(mp3Song);
    if (!isConnected) {
        createConnection();
    }
               }
 
public function onPlayEvent(sAction:String):void {
    if (sAction=='play') {
        btnPlay.enabled = false;
        btnStop.enabled = true;
        btnPause.enabled = true;
        // for the playback
        if (mp3Stream==null) {
            mp3Stream = new NetStream(nc);
        }
        mp3Stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR,
catchAll);
        mp3Stream.addEventListener(IOErrorEvent.IO_ERROR,
catchAll);
        mp3Stream.addEventListener(NetStatusEvent.NET_STATUS,
catchAll);
        mp3Stream.play("mp3:" + mp3Song);
        // for the id3
        if (id3Stream==null) {
            id3Stream = new NetStream(nc);
        }
        id3Stream.client = this;
        id3Stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR,
catchAll);
        id3Stream.play("id3:" + mp3Song);
    }
}

According to Renaun Erickson's blog:

"The NetStream is not dynamic in AS3 and you use the client property to handle method calls from the server. There will be a onPlayerStatus and onId3 method call the server makes on the NetStream object."

 

Report abuse

Related recipes