jeudi 28 juin 2012

Text to Speech in Android

To use the Android Speech to Text API you need to implement the TextToSpeech.OnInitListener interface. In the onInit() method, check the returned status to value, it ca be TextToSpeech.SUCCESS for initialization success or TextToSpeech.ERROR in case of failure. If the initialization succeeded then set your preferred language to US english (or FRANCE for french). 
Note that a language may not be available, check the documentation for more information.
Here is a complete example of how to use the Speech to Text API.
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import java.util.Locale;

public class MainActivity extends Activity implements TextToSpeech.OnInitListener {
 private String TAG = MainActivity.class.getSimpleName();
 private TextToSpeech mTts;
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        mTts = new TextToSpeech(this, this);

 public void onInit(int status) {
        if (status == TextToSpeech.SUCCESS) {            
            int result = mTts.setLanguage(Locale.US);            
            if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Log.e(TAG, "Language data is missing or the language is not supported.");
            } else {                
               String text = "hello, do you hear me?";  
               mTts.speak(text,TextToSpeech.QUEUE_FLUSH, null);
        } else {
            Log.e(TAG, "Could not initialize TextToSpeech.");

Aucun commentaire:

Enregistrer un commentaire