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).

!752

import android.app.*;
import android.content.*;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.widget.Button;
import android.widget.TextView;
import android.util.Log;
import android.media.*;


  static Activity androidContext;
  
  static SpeechRecognizer sr;
  static final String TAG = "MyStt3Activity";
  static boolean loop = true;
  static S language = "en-US";
   
  p {
    androidContext.runOnUiThread(runnable {
      muteAudio();
      
      makeNewObject();
    });
    sleep();
  }
  
  static void makeNewObject() {
    sr = SpeechRecognizer.createSpeechRecognizer(androidContext);
    sr.setRecognitionListener(new listener());        
    go();
  }

  static class listener implements RecognitionListener {
            public void onReadyForSpeech(Bundle params)
            {
                     print("PLEASE SPEAK.");
            }
            public void onBeginningOfSpeech()
            {
                     //print("LISTENING.");
            }
            public void onRmsChanged(float rmsdB)
            {
                     //print("onRmsChanged");
            }
            public void onBufferReceived(byte[] buffer)
            {
                     print("onBufferReceived");
            }
            public void onEndOfSpeech()
            {
                     //print("RECOGNIZING.");
            }
            public void onError(int error)
            {
                     print( "error " +  error); // screw the errors!
                     //if (loop) go();
                     try {
                       sr.destroy();
                     } catch (Throwable e) {
                       print(e);
                     }
                     if (loop)
                       makeNewObject();
            }
            public void onResults(Bundle results)                   
            {
                    
                     //print("onResults " + results);
                     ArrayList<S> data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
                     S bestMatch = data.get(0);
                     print("I heard: " + quote(bestMatch));
                     //print("I heard: " +structure(data));
                     
                     if (match3("goodbye", bestMatch) || match3("bye", bestMatch)) {
                       print("\nGOODBYE!");
                       sr.destroy();
                       return;
                     }
                     
                     if (loop) {
                       sr.stopListening();
                       go();
                     }
            }
            public void onPartialResults(Bundle results)
            {
                    // print("onPartialResults");
                 ArrayList<S> data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
                     S bestMatch = data.get(0);
printStructure(data);
            }
            public void onEvent(int eventType, Bundle params)
            {
                     print("onEvent " + eventType);
            }
   }
   
  static void go() {
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);        
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.
EXTRA_PARTIAL_RESULTS, true);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, language);
    intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,"voice.recognition.test");

    intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,5); 
    sr.startListening(intent);
    //print("started listening");
  }
  
  static void muteAudio() {
    AudioManager amanager = (AudioManager) androidContext.getSystemService(Context.AUDIO_SERVICE);
    amanager.setStreamMute(AudioManager.STREAM_NOTIFICATION, true);
    amanager.setStreamMute(AudioManager.STREAM_ALARM, true);
    amanager.setStreamMute(AudioManager.STREAM_MUSIC, true);
    amanager.setStreamMute(AudioManager.STREAM_RING, true);
    amanager.setStreamMute(AudioManager.STREAM_SYSTEM, true);
  }

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: 688 / 668
Referenced in: [show references]