8 May 2010 6 Comments

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.

Get Adobe Flash player

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.

?View Code ACTIONSCRIPT
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

6 Responses to “Actionscript Text To Speech”

  1. Fardeen 27 May 2010 at 10:57 am #

    Nice ! Thx for your class.

  2. jonathan 28 May 2010 at 12:02 pm #

    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

  3. hcsaba 27 August 2010 at 2:41 pm #

    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()

  4. Pete Shand 27 August 2010 at 5:21 pm #

    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.


Leave a Reply