<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
    initialize="init()"
    layout="vertical" viewSourceURL="mp3_app/index.html">
    <mx:Script>
        <![CDATA[
            
            [Bindable]
            public var nc:NetConnection;
            [Bindable]
            private var isConnected:Boolean = false;
            
            private var id3Stream:NetStream;
            
            private function init():void
            {
                nc = new NetConnection();
                nc.objectEncoding = flash.net.ObjectEncoding.AMF0;
            }
            
            public function createConnection():void
            {
                if( !nc.connected ) {
                    
                    nc.connect( txtRTMPServer.text );
                    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" ) );
                    break;
                    case "NetConnection.Connect.Failed":
                        connectionFailed( new Event( "failed" ) );
                    break;
                    default:
                        trace( "netStatusHandler:code: " + event.info.code );
                    break;
                }
            }
            
            public function connectionSuccess( event:Event ):void
            {
                isConnected = true;

                if( id3Stream == null )
                    id3Stream = new NetStream( nc );
                id3Stream.client = this;
                id3Stream.addEventListener( AsyncErrorEvent.ASYNC_ERROR, catchAll );
                id3Stream.addEventListener( IOErrorEvent.IO_ERROR, catchAll );
                id3Stream.addEventListener( NetStatusEvent.NET_STATUS, catchAll );
                
                id3Stream.play("id3:song4");
            }

            private function catchAll( event:Object ):void
            {
                
                if( event.type == "netStatus" ) {
                    trace( "catchAll:code:" + event.info.code );
                }
                else
                    trace( "catchAll:event:" + event );
            }

            public function connectionFailed( event:Event ):void
            {
                isConnected = true;                
            }

            public function onPlayStatus( event:Object ):void
            {
                for( var b:String in event )        
                    trace( "onPlayStatus: " + b + " - " + event[ b ] );    
            }
            public function onId3( event:Object ):void
            {
                for( var b:String in event ) {
                    this[ "txt_" + b ].text = event[ b ];
                }
            }    
            
        ]]>
    </mx:Script>
    
    <mx:Button 
        id="btnOpenConnection" 
        click="createConnection()"
        label="{ (( !isConnected ) ? 'Create Connection' : 'Close Connection') }" />
    
    <mx:TextInput 
        id="txtRTMPServer" 
        text="rtmp://renaun.com/mp3_app/id3test" 
        width="320" enabled="false"/>

    <mx:Form>
        <mx:FormItem label="Artist">
            <mx:TextInput id="txt_artist" width="200"/>
        </mx:FormItem>    
        <mx:FormItem label="Title">
            <mx:TextInput id="txt_songtitle" width="200"/>
        </mx:FormItem>                
        <mx:FormItem label="Album">
            <mx:TextInput id="txt_album" width="200"/>
        </mx:FormItem>                
        <mx:FormItem label="Year">
            <mx:TextInput id="txt_year" width="40"/>
        </mx:FormItem>                
        <mx:FormItem label="Comment">
            <mx:TextArea id="txt_comment" width="200"/>    
        </mx:FormItem>
    </mx:Form>
</mx:Application>