Advertisement
Guest User

Untitled

a guest
Jun 28th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. webView.setWebChromeClient(new WebChromeClient() {
  2. @Override
  3. public void onPermissionRequest(final PermissionRequest request) {
  4. request.grant(request.getResources());
  5. }
  6.  
  7. private String LOG_TAG = "RecognitionService";
  8. private SpeechRecognizer speech = null;
  9. private Intent recognizerIntent;
  10. public RecognitionService() {
  11. }
  12.  
  13. @Override
  14. public IBinder onBind(Intent intent) {
  15. // TODO: Return the communication channel to the service.
  16. startRecognition();
  17. return null;
  18. }
  19.  
  20.  
  21.  
  22. @Override
  23. public void onCreate() {
  24. Log.i("Test", "RecognitionService: onCreate");
  25. startRecognition();
  26. }
  27.  
  28. private void startRecognition() {
  29. speech = SpeechRecognizer.createSpeechRecognizer(this);
  30. speech.setRecognitionListener(this);
  31. recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
  32. recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE,
  33. "ru-RU");
  34. recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
  35. getPackageName());
  36. recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
  37. RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
  38. recognizerIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3);
  39. speech.startListening(recognizerIntent);
  40. }
  41.  
  42. @Override
  43. public void onBeginningOfSpeech() {
  44. Log.i(LOG_TAG, "onBeginningOfSpeech");
  45. }
  46.  
  47. @Override
  48. public void onBufferReceived(byte[] buffer) {
  49. Log.i(LOG_TAG, "onBufferReceived: " + buffer);
  50. }
  51.  
  52. @Override
  53. public void onEndOfSpeech() {
  54. Log.i(LOG_TAG, "onEndOfSpeech");
  55. }
  56.  
  57. @Override
  58. public void onError(int errorCode) {
  59. String errorMessage = getErrorText(errorCode);
  60. Log.d(LOG_TAG, "FAILED " + errorMessage);
  61. speech.destroy();
  62. startRecognition();
  63. }
  64. @Override
  65. public void onEvent(int arg0, Bundle arg1) {
  66. Log.i(LOG_TAG, "onEvent");
  67. }
  68.  
  69. @Override
  70. public void onPartialResults(Bundle arg0) {
  71. Log.i(LOG_TAG, "onPartialResults");
  72. }
  73.  
  74. @Override
  75. public void onReadyForSpeech(Bundle arg0) {
  76. Log.i(LOG_TAG, "onReadyForSpeech");
  77. }
  78.  
  79. @Override
  80. public void onResults(Bundle results) {
  81. Log.i(LOG_TAG, "onResults");
  82. ArrayList<String> matches = results
  83. .getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
  84. String text = "";
  85. for (String result : matches)
  86. text += result + "n";
  87.  
  88. Toast.makeText(getApplicationContext(),text,Toast.LENGTH_SHORT).show();
  89. speech.destroy();
  90. startRecognition();
  91. }
  92.  
  93.  
  94. public static String getErrorText(int errorCode) {
  95. String message;
  96. switch (errorCode) {
  97. case SpeechRecognizer.ERROR_AUDIO:
  98. message = "Audio recording error";
  99. break;
  100. case SpeechRecognizer.ERROR_CLIENT:
  101. message = "Client side error";
  102. break;
  103. case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS:
  104. message = "Insufficient permissions";
  105. break;
  106. case SpeechRecognizer.ERROR_NETWORK:
  107. message = "Network error";
  108. break;
  109. case SpeechRecognizer.ERROR_NETWORK_TIMEOUT:
  110. message = "Network timeout";
  111. break;
  112. case SpeechRecognizer.ERROR_NO_MATCH:
  113. message = "No match";
  114. break;
  115. case SpeechRecognizer.ERROR_RECOGNIZER_BUSY:
  116. message = "RecognitionService busy";
  117. break;
  118. case SpeechRecognizer.ERROR_SERVER:
  119. message = "error from server";
  120. break;
  121. case SpeechRecognizer.ERROR_SPEECH_TIMEOUT:
  122. message = "No speech input";
  123. break;
  124. default:
  125. message = "Didn't understand, please try again.";
  126. break;
  127. }
  128. return message;
  129. }
  130. @Override
  131. public void onRmsChanged(float rmsdB) {
  132. Log.i(LOG_TAG, "onRmsChanged: " + rmsdB);
  133. }
  134.  
  135.  
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement