Advertisement
Guest User

Untitled

a guest
Jul 20th, 2014
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1. package com.android.speechtest;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Locale;
  5.  
  6. import android.app.Activity;
  7. import android.app.Dialog;
  8. import android.content.ActivityNotFoundException;
  9. import android.content.Intent;
  10. import android.os.Bundle;
  11. import android.speech.RecognizerIntent;
  12. import android.speech.tts.TextToSpeech;
  13. import android.util.Log;
  14. import android.view.Menu;
  15. import android.view.View;
  16. import android.view.View.OnClickListener;
  17. import android.widget.Button;
  18. import android.widget.TextView;
  19. import android.widget.Toast;
  20.  
  21. public class MainActivity extends Activity implements
  22. TextToSpeech.OnInitListener {
  23. protected static final int RESULT_SPEECH = 1;
  24. private TextToSpeech tts;
  25. Toast toast;
  26. TextView textView;
  27. ArrayList<String> text;
  28. Button btnPlay, btnRe, btnExit;
  29. String localeSys;
  30.  
  31. @Override
  32. protected void onCreate(Bundle savedInstanceState) {
  33. super.onCreate(savedInstanceState);
  34. setContentView(R.layout.activity_main);
  35.  
  36. localeSys = Locale.getDefault().toString();
  37. Toast.makeText(getApplicationContext(),
  38. "System locale detected: " + localeSys, Toast.LENGTH_LONG)
  39. .show();
  40. Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
  41.  
  42. intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, localeSys.trim());
  43.  
  44. try {
  45. startActivityForResult(intent, RESULT_SPEECH);
  46.  
  47. } catch (ActivityNotFoundException a) {
  48. Toast t = Toast.makeText(getApplicationContext(),
  49. "Opps! Your device doesn't support Speech to Text",
  50. Toast.LENGTH_LONG);
  51. t.show();
  52. }
  53.  
  54. }
  55.  
  56. @Override
  57. public boolean onCreateOptionsMenu(Menu menu) {
  58. // Inflate the menu; this adds items to the action bar if it is present.
  59. getMenuInflater().inflate(R.menu.main, menu);
  60. return true;
  61. }
  62.  
  63. @Override
  64. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  65. super.onActivityResult(requestCode, resultCode, data);
  66.  
  67. switch (requestCode) {
  68. case RESULT_SPEECH: {
  69. if (resultCode == RESULT_OK && null != data) {
  70.  
  71. text = data
  72. .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
  73.  
  74. Toast.makeText(getApplicationContext(), text.get(0),
  75. Toast.LENGTH_LONG).show();
  76.  
  77. // Create custom dialog object
  78. final Dialog dialog = new Dialog(MainActivity.this);
  79. // Include dialog.xml file
  80. dialog.setContentView(R.layout.custom_dialog);
  81. // Set dialog title
  82. dialog.setTitle(text.get(0));
  83. TextView textV = (TextView) dialog.findViewById(R.id.txtDisp);
  84. textV.setText(text.get(0));
  85. // set values for custom dialog components - text, image and
  86. // button
  87. dialog.show();
  88.  
  89. btnExit = (Button) dialog.findViewById(R.id.btnExit);
  90.  
  91. btnExit.setOnClickListener(new OnClickListener() {
  92. @Override
  93. public void onClick(View v) {
  94. // Close dialog
  95. dialog.dismiss();
  96. System.exit(0);
  97. }
  98. });
  99.  
  100. btnRe = (Button) dialog.findViewById(R.id.btnRestart);
  101.  
  102. btnRe.setOnClickListener(new OnClickListener() {
  103. @Override
  104. public void onClick(View v) {
  105. Intent intent = new Intent();
  106. intent.setClass(getApplicationContext(),
  107. MainActivity.class);
  108. startActivity(intent);
  109. }
  110. });
  111.  
  112. btnPlay = (Button) dialog.findViewById(R.id.btnPlayText);
  113.  
  114. btnPlay.setOnClickListener(new OnClickListener() {
  115. @Override
  116. public void onClick(View v) {
  117. speakOut(text.get(0));
  118. }
  119.  
  120. });
  121.  
  122. }
  123. break;
  124. }
  125.  
  126. }
  127. }
  128.  
  129. @Override
  130. public void onDestroy() {
  131. // Don't forget to shutdown tts!
  132. if (tts != null) {
  133. tts.stop();
  134. tts.shutdown();
  135. }
  136. super.onDestroy();
  137. }
  138.  
  139. @Override
  140. public void onInit(int status) {
  141.  
  142. if (status == TextToSpeech.SUCCESS) {
  143.  
  144. int result = tts.setLanguage(Locale.US);
  145.  
  146. if (result == TextToSpeech.LANG_MISSING_DATA
  147. || result == TextToSpeech.LANG_NOT_SUPPORTED) {
  148. Log.e("TTS", "This Language is not supported");
  149. } else {
  150.  
  151. speakOut(text.get(0));
  152. }
  153.  
  154. } else {
  155. Log.e("TTS", "Initilization Failed!");
  156. }
  157.  
  158. }
  159.  
  160. private void speakOut(String ttsText) {
  161.  
  162. String text = ttsText;
  163.  
  164. tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
  165. }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement