Hello, I was playing with adding Text to Speech to one of my applications and have been working with it using the Android 1.6 SDK. I was hoping to release the code such that it could be used on the earlier 1.5 OS since there are still quite a few of these around - and this is an upgrade. I realize that the TTS won't work on the 1.5 (at least not as I have it here), but I didn't want the app disappearing from the market for those devices.
At any rate, after struggling for a while with this - a lot of trial and error - I came up with what now seems like a simple wrapper to allow for the backward compatibility. This was mostly taken from the Android blog entry http://android-developers.blogspot.com/2009/04/backward-compatibility-for-android.html so I'm not exactly breaking new ground. Also, I'm not using all of the TTS features, hence the short list of "wrapped" methods, expand at your leisure. I'm attaching it here for anyone to use and anyone to comment on if I have done something really stupid. I'll comment back if my testing uncovers anything untoward. Wrapper Class: package com.mydomain.myapp; import java.util.HashMap; import java.util.Locale; import android.content.Context; import android.speech.tts.TextToSpeech; public class WrapTTS { private TextToSpeech mTTS; /* class initialization fails when this throws an exception */ static { try { Class.forName("android.speech.tts.TextToSpeech"); } catch (Exception ex) { throw new RuntimeException(ex); } } /* calling here forces class initialization */ public static void checkAvailable() {} private OnInitListener onInitListener = null; public interface OnInitListener { public abstract void onInit(int status); } public WrapTTS(Context context, WrapTTS.OnInitListener listener) { onInitListener = listener; mTTS = new TextToSpeech(context, new TextToSpeech.OnInitListener() { @Override public void onInit(int status) { onInitListener.onInit(status); } }); } public int setSpeechRate(float speechRate) { return mTTS.setSpeechRate(speechRate); } public void shutdown() { mTTS.shutdown(); } public int speak(String text, int queueMode, HashMap<String, String> params) { return mTTS.speak(text, queueMode, params); } public int setLanguage(Locale loc) { return mTTS.setLanguage(loc); } public int isLanguageAvailable(Locale loc) { return mTTS.isLanguageAvailable(loc); } } The following is used at the top of my main application. private WrapTTS mTts = null; private static boolean mTTSClassAvailable = false; /* establish whether the "TextToSpeech" class is available to us */ static { try { WrapTTS.checkAvailable(); mTTSClassAvailable = true; } catch (Throwable t) { mTTSClassAvailable = false; } } Use the mTTSClassAvailable to know whether to bother to even try to set up the engine. Hopefully it helps someone. Best Regards, Eric -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en