0

I am trying to create a basic app for implementing voice command features and have thus far got some basic functionality working. Currently if I say 'open voice command' this text appears in a 'TextView' field with the id 'result_text'.

I am wanting to pass the text displayed in the 'TextView' into the 'EditText' field called 'TFusername' followed by automatically triggering a button press called 'bVoice' after 1-2 seconds.

Can this be easily implemented into my current code which I have shown below:

public class PocketSphinxActivity extends Activity implements RecognitionListener {


private static final String KWS_SEARCH = "wakeup";

/* Keyword we are looking for to activate menu */
private static final String KEYPHRASE = "open voice command";   //adjust this keyphrase!

private SpeechRecognizer recognizer;
private HashMap<String, Integer> captions;

ListView lv;
TextView tv;
EditText a;
Button b;
Button c;


@Override
public void onCreate(Bundle state) {
    super.onCreate(state);

    // Prepare the data for UI
    captions = new HashMap<String, Integer>();
    captions.put(KWS_SEARCH, R.string.kws_caption);
    setContentView(R.layout.main);
    ((TextView) findViewById(R.id.caption_text))
            .setText("Preparing the recognizer");

    lv = (ListView) findViewById(R.id.lvVoiceReturn);
    a = (EditText) findViewById(R.id.TFusername);
    b = (Button) findViewById(R.id.bVoice);
    c = (Button)findViewById(R.id.Blogin);


    // Recognizer initialization is a time-consuming and it involves IO,
    // so we execute it in async task

    new AsyncTask<Void, Void, Exception>() {
        @Override
        protected Exception doInBackground(Void... params) {
            try {
                Assets assets = new Assets(PocketSphinxActivity.this);
                File assetDir = assets.syncAssets();
                setupRecognizer(assetDir);
            } catch (IOException e) {
                return e;
            }
            return null;
        }

        @Override
        protected void onPostExecute(Exception result) {
            if (result != null) {
                ((TextView) findViewById(R.id.caption_text))
                        .setText("Failed to init recognizer " + result);
            } else {
                switchSearch(KWS_SEARCH);
            }
        }
    }.execute();
}

@Override
public void onDestroy() {
    super.onDestroy();
    recognizer.cancel();
    recognizer.shutdown();
}

/**
 * In partial result we get quick updates about current hypothesis. In
 * keyword spotting mode we can react here, in other modes we need to wait
 * for final result in onResult.
 */
@Override
public void onPartialResult(Hypothesis hypothesis) {
    if (hypothesis == null)
        return;

    String text = hypothesis.getHypstr();
    ((TextView) findViewById(R.id.result_text)).setText(text);
}

/**
 * This callback is called when we stop the recognizer.
 */
@Override
public void onResult(Hypothesis hypothesis) {
    ((TextView) findViewById(R.id.result_text)).setText("");
    if (hypothesis != null) {
        String text = hypothesis.getHypstr();
        makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
    }
}

@Override
public void onBeginningOfSpeech() {
}

/**
 * We stop recognizer here to get a final result
 */
@Override
public void onEndOfSpeech() {
    if (!recognizer.getSearchName().equals(KWS_SEARCH))
        switchSearch(KWS_SEARCH);
}

private void switchSearch(String searchName) {
    recognizer.stop();

    // If we are not spotting, start listening with timeout (10000 ms or 10 seconds).
    if (searchName.equals(KWS_SEARCH))
        recognizer.startListening(searchName);
    else
        recognizer.startListening(searchName, 10000);

    String caption = getResources().getString(captions.get(searchName));
    ((TextView) findViewById(R.id.caption_text)).setText(caption);
}

private void setupRecognizer(File assetsDir) throws IOException {
    // The recognizer can be configured to perform multiple searches
    // of different kind and switch between them

    recognizer = defaultSetup()
            .setAcousticModel(new File(assetsDir, "en-us-ptm"))
            .setDictionary(new File(assetsDir, "cmudict-en-us.dict"))

                    // To disable logging of raw audio comment out this call (takes a lot of space on the device)
            .setRawLogDir(assetsDir)

                    // Threshold to tune for keyphrase to balance between false alarms and misses
            .setKeywordThreshold(1e-45f)

                    // Use context-independent phonetic search, context-dependent is too slow for mobile
            .setBoolean("-allphone_ci", true)

            .getRecognizer();
    recognizer.addListener(this);

    /** In your application you might not need to add all those searches.
     * They are added here for demonstration. You can leave just one.
     */

    // Create keyword-activation search.
    recognizer.addKeyphraseSearch(KWS_SEARCH, KEYPHRASE);

}

@Override
public void onError(Exception error) {
    ((TextView) findViewById(R.id.caption_text)).setText(error.getMessage());
}

@Override
public void onTimeout() {
    switchSearch(KWS_SEARCH);
}

I will really appreciate some help from anyone who can implement this easily, Thanks in advanced!

N MC
  • 207
  • 1
  • 10
  • If someone can kindly tell me where in the code to modify and how to modify that will be appreciated. i.e how to make the textview 'result_text' into a string and then pass it into the edittext field 'TFusername' – N MC May 06 '16 at 05:00

2 Answers2

0

You can set the text of an edittext exactly the same way you set it in a textview, so just call a.setText(textFromYourTextView) where ever you want it to be called. You can then use a handler to delay an action by a couple of seconds as shown in this example:

Execute function after 5 seconds in Android

Then within the handler code call the onclick of your button using the .callOnClick() method.

Community
  • 1
  • 1
Maslada
  • 141
  • 1
  • 5
  • when you say `a.setText(textFromYourTextView)` what should I be writting exactly inside of the brackets? is it 'open voice command' or set as as if statements? – N MC May 06 '16 at 03:05
  • Well if you want it to be exactly what is in your textview then textFromYourTextView variable I used as an example would be: textFromYourTextView = textView.getText().toString(); – Maslada May 06 '16 at 03:08
  • I think you have almost helped me solve this problem, Where in the code would I need to write this line of code and do I need to define the variable 'textFromYourTextView' earlier on in the code. – N MC May 06 '16 at 04:03
  • would it be something similar to this: `@Override public void onResult(Hypothesis hypothesis) { ((TextView) findViewById(R.id.result_text)).setText(""); if (hypothesis != null) { String text = hypothesis.getHypstr(); makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show(); a.setText(TextView result_text); } }` Only issue is I have error..? – N MC May 06 '16 at 04:14
  • Hi, maslada, i'll really appreciate your help in fixing this issue please – N MC May 06 '16 at 05:57
0

I managed to fix this problem,

In the above code wherever it referred to TextView and the result_field, I just replaced it with EditText and TFusername

Hope this makes it clearer for anyone else who stumbles across the same issue

N MC
  • 207
  • 1
  • 10