<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
  backgroundColor="#ffffff" backgroundGradientColors="#ffffff,#ffffff" styleName="plain"
  paddingTop="30" horizontalAlign="center" viewSourceURL="srcview/index.html">
  
  <mx:Script>
    import mx.controls.Alert;
    import mx.utils.URLUtil;

    private function checkStatus(fileid:String):void
    {
      currentFileid = fileid;
      
      var t:Timer = new Timer(100, 1);
      t.addEventListener(TimerEvent.TIMER, function(event:TimerEvent):void {
        req.action = "status";
        req.fileid = currentFileid;
        srv.send(req);
      });
      t.start();
    }
  </mx:Script>
  
  <mx:String id="currentFileid"/>
  
  <mx:Object id="req" username="james@jamesward.org" password="password"/>

  <mx:HTTPService id="srv" url="http://ws.ispeech.org/api/rest/1.5" method="POST" resultFormat="text">
    <mx:result>
    <![CDATA[
        var r:Object = URLUtil.stringToObject(event.result as String, "&");
        
        if (r != null)
        {
          if (r.result == "success")
          {
            if (r.status == "finished")
            {
              var stream:URLRequest = new URLRequest(srv.url);
              stream.method = URLRequestMethod.POST;
              stream.data = new URLVariables();
              stream.data.username = req.username;
              stream.data.password = req.password;
              stream.data.action = "download";
              if (r.fileid != null)
              {
                stream.data.fileid = r.fileid;
              }
              else
              {
                stream.data.fileid = currentFileid;
              }
              
              var sound:Sound = new Sound(stream);
              sound.play();
            }
            else if ((r.status == "waiting") || (r.status == "working"))
            {
              if (r.fileid != null)
              {
                checkStatus(r.fileid);
              }
              else
              {
                checkStatus(currentFileid);
              }
            }
          }
          else
          {
            Alert.show((r.message as String).replace(/\+/g, " "), "Error Converting Text to Speech");
          }
        }
    ]]>
    </mx:result>
  </mx:HTTPService>
  
  <mx:Panel title="Convert Text to Speech with iSpeech">
    
    <mx:TextArea id="ta" width="350" height="100" text="This is a test.  Please raise your hand if you can hear me.  Tour de Flex is cool!" maxChars="300"/>

    <mx:ControlBar>
      <mx:Label text="Voice:"/>
      
      <mx:ComboBox id="voice">
        <mx:dataProvider>
          <mx:Array>
            <mx:Object label="Free Female" value="engfemale2"/>
            <mx:Object label="Premium Female" value="engfemale3"/>
            <mx:Object label="Premium Male" value="engmale1"/>
          </mx:Array>
        </mx:dataProvider>
      </mx:ComboBox>
      
      <mx:Button label="Read The Text">
        <mx:click>
          req.action = "convert";
          req.text = ta.text;
          req.voice = voice.selectedItem.value;
          srv.send(req);
        </mx:click>
      </mx:Button>
      
      </mx:ControlBar>
  </mx:Panel>
  
</mx:Application>