Flash Text-To-Speech

I stumbled across a post by Viral Patel where he demonstrates a hidden Google gem for converting text to speech. For some reason, the 13 year old in me always gets a kick out this sort of thing. I remember back in my Commodore 64 days, sitting in the basement with my brothers and using S.A.M. to say anything naughty that we could come up with and laughing till our sides hurt. So I decided that I would put a Flex wrapper around the Google API and relive the glory days. Check it out, have fun, and take a look at the code below.






<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="320" height="240">
    <mx:Button x="93" y="174" label="Say It!" click="handlePlayClick()" width="134" height="50" fontSize="22"/>
    <mx:TextArea x="10" y="10" width="300" height="134" maxChars="100" wordWrap="true" editable="true" enabled="true" change="resetPlayFlag()" id="message" text="Shall we play a game? "/>
    <mx:Label x="102.5" y="148" text="100 characters max"/>
    
    <mx:Script>
        <![CDATA[
            import mx.controls.Alert;
            private var newMessage:Boolean = true;
            private var loader:URLLoader;
            private var voiceMessage:Sound;
            private var googleTTSURL:String = "http://translate.google.com/translate_tts";
            
            private function resetPlayFlag():void {
                newMessage = true;
            }
            
            private function handlePlayClick():void {
                trace("play click");
                if (newMessage) {
                    var variables:URLVariables = new URLVariables();
                    variables.q = message.text;
                    
                    var req:URLRequest = new URLRequest(googleTTSURL);
                    req.method = URLRequestMethod.GET;
                    req.data = variables;
                    
                    voiceMessage = new Sound();
                    voiceMessage.addEventListener(Event.COMPLETE, handleServerResponse);
                    voiceMessage.addEventListener(IOErrorEvent.IO_ERROR, handleServerError);
                    voiceMessage.load(req);
                } else {
                    playMessage();
                }
            }
            
            private function handleServerResponse(event:Event):void {
                trace("got it!);");
                newMessage = false;
                playMessage();
            }
            
            private function playMessage():void {
                trace("play message");
                voiceMessage.play();
            }
            
            private function handleServerError(event:IOErrorEvent):void {
                trace("problem");
                Alert.show("Server Connection Error: " + event.text);
            }
        ]]>
    </mx:Script>
</mx:Application>

Comments

Rider said…
It doesn´t work "Server Connection Error: Error #2032"
CodeJockey said…
Are you behind a restrictive firewall or proxy that may be blocking the .mp3?
Enrico Piccini said…
Also to me the same error :(

Popular Posts