Guest User

Untitled

a guest
Mar 18th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. public class yesOunoActivity extends Activity implements OnInitListener{
  2. ImageView yes;
  3. ImageView no;
  4. public TextToSpeech tts;
  5. private int MY_DATA_CHECK_CODE = 0;
  6.  
  7. @Override
  8. public void onCreate(Bundle savedInstanceState) {
  9.  
  10. super.onCreate(savedInstanceState);
  11. Intent checkIntent = new Intent();
  12. checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
  13. startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
  14.  
  15. tts = new TextToSpeech(this, this);
  16.  
  17. setContentView(R.layout.yesorno);
  18.  
  19. yes = (ImageView) findViewById(R.id.yes);
  20. no = (ImageView) findViewById(R.id.no);
  21.  
  22.  
  23. yes.setClickable(true);
  24. yes.setOnTouchListener(new OnTouchListener() {
  25. @Override
  26. public boolean onTouch(View v, MotionEvent arg1) {
  27.  
  28. if (arg1.getAction() == android.view.MotionEvent.ACTION_DOWN) {
  29. tts.speak("yes!", TextToSpeech.QUEUE_ADD, null);
  30. }
  31. return true;
  32. }
  33. });
  34.  
  35.  
  36. no.setClickable(true);
  37. no.setOnTouchListener(new OnTouchListener() {
  38. @Override
  39. public boolean onTouch(View v, MotionEvent arg1) {
  40.  
  41. if (arg1.getAction() == android.view.MotionEvent.ACTION_DOWN) {
  42.  
  43. //Intent myIntent = new Intent(v.getContext(), ParametrosActivity.class);
  44. tts.speak("no!", TextToSpeech.QUEUE_ADD, null);
  45. }
  46. return true;
  47. }
  48.  
  49. });
  50.  
  51.  
  52. }
  53.  
  54.  
  55. @Override
  56. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  57. if (requestCode == MY_DATA_CHECK_CODE) {
  58. if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
  59. // success, create the TTS instance
  60. tts = new TextToSpeech(this, this);
  61. } else {
  62. // missing data, install it
  63. //ATTENTION: BELOW THIS GIVES ME PROBLEMS SINCE IT OPENS MARKET
  64. //AND I HAVE TO HIT THE BACK BUTTON, THEN, IT SPEAKS!
  65. //BTW TTS ENGINE "IS" INSTALLED!!
  66. Intent installIntent = new Intent();
  67. installIntent
  68. .setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
  69. startActivity(installIntent);
  70. }
  71. }
  72.  
  73. }
  74.  
  75. @Override
  76. public void onInit(int status) {
  77. if (status == TextToSpeech.SUCCESS) {
  78. } else if (status == TextToSpeech.ERROR) {
  79. }
  80. }
  81.  
  82. @Override
  83. public void onDestroy() {
  84. if (tts != null) {
  85. tts.stop();
  86. tts.shutdown();
  87. }
  88. super.onDestroy();
  89. System.gc();
  90. }
  91.  
  92. }
  93.  
  94. @Override
  95. protected void onActivityResult(
  96. int requestCode, int resultCode, Intent data) {
  97. if (requestCode == MY_DATA_CHECK_CODE) {
  98. /*
  99. if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
  100. // success, create the TTS instance
  101. mTts = new TextToSpeech(this, this);
  102. } else {
  103. // missing data, install it
  104. Intent installIntent = new Intent();
  105. installIntent.setAction(
  106. TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
  107. startActivity(installIntent);
  108. }
  109. */
  110. if (mTts==null) {
  111. Intent installIntent = new Intent();
  112. installIntent.setAction(
  113. TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
  114. startActivity(installIntent);
  115. }
  116. mTts = new TextToSpeech(this, this);
  117. }
  118. }
  119.  
  120. // in Activity
  121. private static TextToSpeech mTts;
  122. .
  123. .
  124. .
  125. // in onCreate()
  126. mTts = new TextToSpeech(this, this);
  127. .
  128. .
  129. .
  130. // in onDestroy()
  131. if (mTts != null) {
  132. mTts.stop();
  133. mTts.shutdown();
  134. mTts = null;
  135. }
Add Comment
Please, Sign In to add comment