Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

125
LINES

< > BotCompany Repo | #1001252 // Android: Direct SpeechRecognizer test with muting & repeat, english (trying to fix error 8)

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (1417L/10K/32K).

1  
!752
2  
3  
import android.app.*;
4  
import android.content.*;
5  
import android.os.Bundle;
6  
import android.view.View;
7  
import android.view.View.OnClickListener;
8  
import android.speech.RecognitionListener;
9  
import android.speech.RecognizerIntent;
10  
import android.speech.SpeechRecognizer;
11  
import android.widget.Button;
12  
import android.widget.TextView;
13  
import android.util.Log;
14  
import android.media.*;
15  
16  
17  
  static Activity androidContext;
18  
  
19  
  static SpeechRecognizer sr;
20  
  static final String TAG = "MyStt3Activity";
21  
  static boolean loop = true;
22  
  static S language = "en-US";
23  
   
24  
  p {
25  
    androidContext.runOnUiThread(runnable {
26  
      muteAudio();
27  
      
28  
      makeNewObject();
29  
    });
30  
    sleep();
31  
  }
32  
  
33  
  static void makeNewObject() {
34  
    sr = SpeechRecognizer.createSpeechRecognizer(androidContext);
35  
    sr.setRecognitionListener(new listener());        
36  
    go();
37  
  }
38  
39  
  static class listener implements RecognitionListener {
40  
            public void onReadyForSpeech(Bundle params)
41  
            {
42  
                     print("PLEASE SPEAK.");
43  
            }
44  
            public void onBeginningOfSpeech()
45  
            {
46  
                     //print("LISTENING.");
47  
            }
48  
            public void onRmsChanged(float rmsdB)
49  
            {
50  
                     //print("onRmsChanged");
51  
            }
52  
            public void onBufferReceived(byte[] buffer)
53  
            {
54  
                     print("onBufferReceived");
55  
            }
56  
            public void onEndOfSpeech()
57  
            {
58  
                     //print("RECOGNIZING.");
59  
            }
60  
            public void onError(int error)
61  
            {
62  
                     print( "error " +  error); // screw the errors!
63  
                     //if (loop) go();
64  
                     try {
65  
                       sr.destroy();
66  
                     } catch (Throwable e) {
67  
                       print(e);
68  
                     }
69  
                     if (loop)
70  
                       makeNewObject();
71  
            }
72  
            public void onResults(Bundle results)                   
73  
            {
74  
                    
75  
                     //print("onResults " + results);
76  
                     ArrayList<S> data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
77  
                     S bestMatch = data.get(0);
78  
                     print("I heard: " + quote(bestMatch));
79  
                     //print("I heard: " +structure(data));
80  
                     
81  
                     if (match3("goodbye", bestMatch) || match3("bye", bestMatch)) {
82  
                       print("\nGOODBYE!");
83  
                       sr.destroy();
84  
                       return;
85  
                     }
86  
                     
87  
                     if (loop) {
88  
                       sr.stopListening();
89  
                       go();
90  
                     }
91  
            }
92  
            public void onPartialResults(Bundle results)
93  
            {
94  
                    // print("onPartialResults");
95  
                 ArrayList<S> data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
96  
                     S bestMatch = data.get(0);
97  
printStructure(data);
98  
            }
99  
            public void onEvent(int eventType, Bundle params)
100  
            {
101  
                     print("onEvent " + eventType);
102  
            }
103  
   }
104  
   
105  
  static void go() {
106  
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);        
107  
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
108  
    intent.putExtra(RecognizerIntent.
109  
EXTRA_PARTIAL_RESULTS, true);
110  
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, language);
111  
    intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,"voice.recognition.test");
112  
113  
    intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,5); 
114  
    sr.startListening(intent);
115  
    //print("started listening");
116  
  }
117  
  
118  
  static void muteAudio() {
119  
    AudioManager amanager = (AudioManager) androidContext.getSystemService(Context.AUDIO_SERVICE);
120  
    amanager.setStreamMute(AudioManager.STREAM_NOTIFICATION, true);
121  
    amanager.setStreamMute(AudioManager.STREAM_ALARM, true);
122  
    amanager.setStreamMute(AudioManager.STREAM_MUSIC, true);
123  
    amanager.setStreamMute(AudioManager.STREAM_RING, true);
124  
    amanager.setStreamMute(AudioManager.STREAM_SYSTEM, true);
125  
  }

Author comment

Began life as a copy of #1001131

download  show line numbers  debug dex  old transpilations   

Travelled to 17 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, crvwmplrxojx, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, nbgitpuheiab, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, ufhdzwgrioxs, vouqrxazstgt

No comments. add comment

Snippet ID: #1001252
Snippet name: Android: Direct SpeechRecognizer test with muting & repeat, english (trying to fix error 8)
Eternal ID of this version: #1001252/1
Text MD5: c0d98ac1bf8a4181a56bacc88d1eee46
Transpilation MD5: b8e7153aac4cc455eda4c293fb555745
Author: stefan
Category:
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-08-31 04:30:49
Source code size: 4368 bytes / 125 lines
Pitched / IR pitched: No / No
Views / Downloads: 743 / 738
Referenced in: [show references]