Tuesday, February 23, 2010

Steps to create a Sample TextToSpeech

The following steps to help you create a Sample TextToSpeech(TTS) Application

1. Create a MainActivity which is extends Activity and Implements OnInitListener interface
2. Check the Text Speech Feature using the " TextToSpeech.Engine.ACTION_CHECK_TTS_DATA " or Install the Feature. Ensure your API 1.6 or greater than the 1.6
Because starting with Android 1.6 (API Level 4), the Android platform includes a new Text-to-Speech (TTS) capability. Also known as "speech synthesis", TTS enables your Android device to "speak" text of different languages.
3. Create TextToSpeech instance or installed the TextToSpeech API and create instance.
4. Configure with locale Implementation
5. Call the speak() to test the TTS


Create a MainActivity which is extends Activity and Implements OnInitListener interface
public class MainActivity extends Activity implements OnInitListener {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        }
        @Override
    public void onInit(int status) {
    }
}




2. Check the Text Speech Feature using the " TextToSpeech.Engine.ACTION_CHECK_TTS_DATA " or Install the Feature. Ensure your API 1.6 or greater than the 1.6
Because starting with Android 1.6 (API Level 4), the Android platform includes a new Text-to-Speech (TTS) capability. Also known as "speech synthesis", TTS enables your Android device to "speak" text of different languages.
    Intent checkIntent = new Intent();
        checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
        startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);

3. Create TextToSpeech instance or installed the TextToSpeech API and create instance.
private TextToSpeech mTts;
    protected void onActivityResult(
            int requestCode, int resultCode, Intent data) {
        if (requestCode == MY_DATA_CHECK_CODE) {
            if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
                // success, create the TTS instance
                mTts = new TextToSpeech(this, this);
            } else {
                // missing data, install it
                Intent installIntent = new Intent();
                installIntent.setAction(
                    TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                startActivity(installIntent);
            }
        }
    }


4. Configure with locale Implementation
    @Override
    public void onInit(int status) {
        mTts.setLanguage(Locale.US);   
    }


5. Call the speak() to test the TTS
    mTts.speak("hai this is for testing, Karhtik is testing this application", TextToSpeech.QUEUE_FLUSH, null);

6. Sample Code
public class MainActivity extends Activity implements OnInitListener {
    int MY_DATA_CHECK_CODE = 1;
    private TextToSpeech mTts;
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Intent checkIntent = new Intent();
        checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
        startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
    }
   

    protected void onActivityResult(
            int requestCode, int resultCode, Intent data) {
        if (requestCode == MY_DATA_CHECK_CODE) {
            if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
                // success, create the TTS instance
                mTts = new TextToSpeech(this, this);
            } else {
                // missing data, install it
                Intent installIntent = new Intent();
                installIntent.setAction(
                    TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                startActivity(installIntent);
            }
        }
    }

    @Override
    public void onInit(int status) {
        mTts.setLanguage(Locale.US);
        mTts.speak("hai this is for testing, Karhtik is testing this application", TextToSpeech.QUEUE_FLUSH, null);
    }
}


Hope this is helpful for you.

No comments:

Post a Comment