-3
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_preview);
        touch_color = (TextView) findViewById(R.id.touch_color);


        picTaken = (ImageView) findViewById(R.id.picTaken);

      //  bitmap = fixRotation(MainActivity.IMG_FILE);
        //picTaken.setImageBitmap(bitmap);

        String toSpeak = touch_color.getText().toString();
        t1.speak(toSpeak, TextToSpeech.QUEUE_ADD, null);


        t1 = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status != TextToSpeech.ERROR) {
                    t1.setLanguage(Locale.US);
                    t1.setLanguage(Locale.UK);

                }

            }
        });

    }

    public void onPause() {
        if (t1 != null) {
            t1.stop();
            t1.shutdown();
        }
        super.onPause();
    }

 Run c

I want to read text field without clicking on button in android using text to speech what I actually want is that, when activity is launch text to speech converter read text show on textfield can anyone help? in used this code but it gives error i want to replace this line of code "t1.speak(toSpeak,TextToSpeech.QUEUE_FLUSH, null);" Please help.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

2 Answers2

0

You can use this link, there is some useful information about text to speech.

  private void promptSpeechInput() {
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
            getString(R.string.speech_prompt));
    try {
        startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
    } catch (ActivityNotFoundException a) {
        Toast.makeText(getApplicationContext(),
                getString(R.string.speech_not_supported),
                Toast.LENGTH_SHORT).show();
    }
}
Shaig Khaligli
  • 4,955
  • 5
  • 22
  • 32
0

You can use the below code snippet to implement Text to Speech converter;

 public class MainActivity extends Activity {
 TextToSpeech t1;
 EditText ed1;     
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  ed1=(EditText)findViewById(R.id.editText);      
  t1=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
     @Override
     public void onInit(int status) {
        if(status != TextToSpeech.ERROR) {
           t1.setLanguage(Locale.UK);
        }
     }
  });


  String toSpeak = ed1.getText().toString();
  Toast.makeText(getApplicationContext(), toSpeak,Toast.LENGTH_SHORT).show();
  t1.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);

}

public void onPause(){
  if(t1 !=null){
     t1.stop();
     t1.shutdown();
  }
  super.onPause();
}
}
Jaydroid
  • 334
  • 3
  • 13