Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.56 KB | None | 0 0
  1. public class SpeechRecognizer extends Activity implements OnInitListener{
  2. private static final int REQUEST_CODE = 1234;
  3. Button Start;
  4. TextView Speech;
  5. Dialog match_text_dialog;
  6. ListView textlist;
  7. String userRequest;
  8. private TextToSpeech tts;
  9. ArrayList<String> matches_text;
  10. private boolean initialized = false;
  11. private String queuedText;
  12. ArrayList<String> EnquiryList;
  13. //ArrayList<String> questionsList;
  14.  
  15. @Override
  16. protected void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.activity_speech_recognizer);
  19. // initQuestions();
  20.  
  21. Start = (Button)findViewById(R.id.start_reg);
  22. Speech = (TextView)findViewById(R.id.speech);
  23. tts = new TextToSpeech(this /* context */, this /* listener */);
  24. Start.setOnClickListener(new OnClickListener() {
  25. @Override
  26. public void onClick(View v) {
  27. if(isConnected()){
  28. callSpeechRecognition();
  29. }
  30. else{
  31. Toast.makeText(getApplicationContext(), "Please Connect to Internet", Toast.LENGTH_LONG).show();
  32. }}
  33. });
  34. }
  35. private void callSpeechRecognition()
  36. {
  37. Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
  38. intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
  39. RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
  40. startActivityForResult(intent, REQUEST_CODE);
  41. }
  42. public boolean isConnected()
  43. {
  44. ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
  45. NetworkInfo net = cm.getActiveNetworkInfo();
  46. if (net!=null && net.isAvailable() && net.isConnected()) {
  47. return true;
  48. } else {
  49. return false;
  50. }
  51. }
  52. private void callEnquiryForm() {
  53. // TODO Auto-generated method stub
  54. Intent intent = new Intent(getBaseContext(), CreateEnquiry.class);
  55. startActivity(intent);
  56. }
  57. @Override
  58. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  59. if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
  60. match_text_dialog = new Dialog(SpeechRecognizer.this);
  61. match_text_dialog.setContentView(R.layout.dialog_matches);
  62. match_text_dialog.setTitle("Select Matching Text");
  63. textlist = (ListView)match_text_dialog.findViewById(R.id.list);
  64. matches_text = data
  65. .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
  66. ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
  67. android.R.layout.simple_list_item_1, matches_text);
  68. textlist.setAdapter(adapter);
  69. textlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  70. @Override
  71. public void onItemClick(AdapterView<?> parent, View view,
  72. int position, long id) {
  73. Speech.setText("You have said " + matches_text.get(position));
  74. String userRequest=matches_text.get(position);
  75. QuestionsList enquiryList=new QuestionsList();
  76. EnquiryList=enquiryList.loadQuestions();
  77. if((userRequest.contains("enquiry"))||(userRequest.contains("query")))
  78. {
  79. //callEnquiryForm();
  80. processEnquiry();
  81. }
  82. match_text_dialog.hide();
  83. }
  84. });
  85. match_text_dialog.show();
  86. }
  87. super.onActivityResult(requestCode, resultCode, data);
  88. }
  89. private void processEnquiry() {
  90. // TODO Auto-generated method stub
  91. for(int i=0;i<EnquiryList.size();i++)
  92. {
  93. speak(EnquiryList.get(i).toString());
  94. callSpeechRecognition();
  95. }
  96. }
  97. @Override
  98. public void onInit(int status) {
  99. if (status == TextToSpeech.SUCCESS) {
  100. initialized = true;
  101. tts.setLanguage(Locale.ENGLISH);
  102. if (queuedText != null) {
  103. speak(queuedText);
  104. }
  105. }
  106. }
  107.  
  108. public void speak(String text) {
  109. // If not yet initialized, queue up the text.
  110. if (!initialized) {
  111. queuedText = text;
  112. return;
  113. }
  114. queuedText = null;
  115. // Before speaking the current text, stop any ongoing speech.
  116. //tts.stop();
  117. // Speak the text.
  118. tts.speak(text, TextToSpeech.QUEUE_ADD, null);
  119. }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement