Actionscript Text To Speech
Ever wanted to add text to speech functionality to your Actionscript project?
Well now you can thanks to this cool little as3 class I wrote last night.
So how does it work?
Google hasn’t released an official API for their text to speech engine, however if you query: http://translate.google.com/translate_tts?tl=en&q=this%20is%20a%20test an mp3 will be produced which speaks whatever q equals. There is however a limit to how many characters will be accepted in one query. TextToSpeech.as gets around this limitation by splitting your request up into smaller more manageable parts and then requests them one at a time. TextToSpeech.as also fixes the delay between clips on playback by starting playback of the next segment just before the current one finishes.
TextToSpeech.as simply bridges the gap between the Google text to speech engine and your as3 project. The example below imports TextToSpeech.as, loads a string (the first paragraph of this blog) and then plays it once the first segment has been converted and downloaded.
1 2 3 4 5 6 7 8 9 10 11 12 | import ps.GText2Speech.TextToSpeech; import ps.GTranslate.Language; textToSpeech = new TextToSpeech(); textToSpeech.Lang = Language.ENGLISH; textToSpeech.addEventListener("FirstClipLoaded", beginPlaying); textToSpeech.load('Ever wanted to add text to speech functionality to your Actionscript project? Well now you can thanks to Google and this awesome little as3 class I’ve written.'); function beginPlaying(event:Event):void { textToSpeech.play(); } |
Because TextToSpeech.as relies on the Google TextToSpeech engine it does mean that this could stop working in the future if Google decides to change or remove the URL. I guess the best way would be to actually do everything within actionscript, however this is easier said than done, so for the time being this is the best option we have.
You can download the source and example files here.
If anyone has additional feature requests please post them below.
Enjoy!
——————————————————
Update – Leading on from this post I’ve added a TextToSpeech accessibility class: Read More

Nice ! Thx for your class.
This is really nice… a feature I think would be cool is caching. Maybe something that runs the entire spectrum of the application and can be included in its assest or on the http domain of the application.
Rock on
Hi,
This don’t work in Opera and IE for me
Error #2044: Unhandled IOErrorEvent:. text=Error #2032: Stream Error.
at net.peteshand::TextToSpeech/loadSpeech()
at net.peteshand::TextToSpeech/load()
at main()
yeah I been told about this… just don’t have time to look into it right now…
If you or anyone for that matter fixes it let me know.