abbyrockss

Untitled

May 20th, 2014
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. private TextToSpeech tts;
  2. private Button btnSpeak;
  3. private EditText txtText;
  4.  
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_main);
  9. btnSpeak = (Button) findViewById(R.id.button1);
  10. btnSpeak.setOnClickListener(this);
  11. txtText = (EditText) findViewById(R.id.editText1);
  12. tts = new TextToSpeech(this, this);
  13.  
  14. }
  15.  
  16. @Override
  17. public boolean onCreateOptionsMenu(android.view.Menu menu) {
  18. // TODO Auto-generated method stub
  19. super.onCreateOptionsMenu(menu);
  20. MenuInflater blowup = getMenuInflater();
  21. blowup.inflate(R.menu.cool_menu, menu);
  22. return true;
  23. }
  24.  
  25.  
  26. @Override
  27. protected void onDestroy() {
  28. // TODO Auto-generated method stub
  29.  
  30. if (tts != null) {
  31. tts.stop();
  32. tts.shutdown();
  33. }
  34. super.onDestroy();
  35. }
  36.  
  37. @Override
  38. public void onInit(int status) {
  39. // TODO Auto-generated method stub
  40. if (status != TextToSpeech.ERROR) {
  41.  
  42. tts.setLanguage(Locale.ENGLISH);
  43.  
  44. }
  45.  
  46. }
  47.  
  48. @Override
  49. public void onClick(View arg0) {
  50. // TODO Auto-generated method stub
  51.  
  52.  
  53. speakOut();
  54. }
  55.  
  56. private void speakOut() {
  57.  
  58. String text= txtText.getText().toString();
  59. String blank = "";
  60. if(text.equals(blank)){
  61. Toast.makeText(getApplicationContext(), "Enter Text First", Toast.LENGTH_SHORT).show();
  62. }
  63.  
  64. tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
  65. }
Advertisement
Add Comment
Please, Sign In to add comment