Anjali Patel
Anjali Patel

Reputation: 825

How to get past information while converting speech to text in android?

As i am developing an android app and wants to convert speech into text, i am using built-in Google speech input activity to convert voice into text. I need past information but it continuously get cleared i got only current response. How need to handle same as google voice keyboard. As i talk it included to current String instep of clear.

MainActivity .java

public class MainActivity extends AppCompatActivity
  {
     private EditText txtSpeechInput;
     private ImageButton btnSpeak;
     private final int REQ_CODE_SPEECH_INPUT = 100;

 @Override
  protected void onCreate(Bundle savedInstanceState)
   {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      txtSpeechInput =  findViewById(R.id.txtSpeechInput);
      btnSpeak = (ImageButton) findViewById(R.id.btnSpeak);
    btnSpeak.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            promptSpeechInput();
        }
    });
   private void promptSpeechInput()
      {
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
            getString(R.string.speech_prompt));
    intent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS, 20000000);

  try
    {
        startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
    }
    catch (ActivityNotFoundException a)
    {
        Toast.makeText(getApplicationContext(),
                getString(R.string.speech_not_supported),
                Toast.LENGTH_SHORT).show();
    }
}
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode)
    {
        case REQ_CODE_SPEECH_INPUT:
        {
            if (resultCode == RESULT_OK && null != data)
            {

              final ArrayList<String> result= data
                        .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);


                txtSpeechInput.setText(result.get(0));
            }
            break;
        }

    }
}

Upvotes: 0

Views: 1065

Answers (3)

user14322221
user14322221

Reputation: 1

public class MainActivity extends AppCompatActivity {

private SpeechRecognizer speechRecognizer;
private Intent intentRecognizer;
private EditText txtSpeechInput;

private ImageButton btnSpeak;

//this is the string in which words get stored and past strings with left to right cursor. String previous = " ";

// ArrayList result = null;

private final int REQ_CODE_SPEECH_INPUT = 100;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate( savedInstanceState );
    setContentView( R.layout.activity_main );

    // ActivityCompat.requestPermissions( this, new String[]{Manifest.permission.RECORD_AUDIO}, PackageManager.PERMISSION_GRANTED );


    txtSpeechInput = findViewById( R.id.ed );


    btnSpeak = (ImageButton) findViewById( R.id.iButton );



    btnSpeak.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            promptSpeechInput();


        }
    } );

}

private void promptSpeechInput() {
    Intent intent = new Intent( RecognizerIntent.ACTION_RECOGNIZE_SPEECH );
    intent.putExtra( RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM );
    intent.putExtra( RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault() );
    intent.putExtra( RecognizerIntent.EXTRA_PROMPT,
            getString( R.string.speech_prompt ) );

    intent.putExtra( RecognizerIntent.EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS, 20000000 );



    try {
        startActivityForResult( intent, REQ_CODE_SPEECH_INPUT );
    } catch (ActivityNotFoundException a) {
        Toast.makeText( getApplicationContext(),
                getString( R.string.speech_not_supported ),
                Toast.LENGTH_SHORT ).show();
    }

}


protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult( requestCode, resultCode, data );

    switch (requestCode) {
        case REQ_CODE_SPEECH_INPUT: {
            if (resultCode == RESULT_OK && null != data) {

                final ArrayList<String> result = data
                        .getStringArrayListExtra( RecognizerIntent.EXTRA_RESULTS );

//this is the real problem. txtSpeechInput.setText( previous + " " + result.get( 0 ) );

                previous = txtSpeechInput.getText().toString();

                txtSpeechInput.setText( previous );




            }
            break;
        }

    }
}

}

Upvotes: 0

Palak Bansal
Palak Bansal

Reputation: 840

The words get stored in Arraylists.

You can see an example of the implementation here, it works fine. The app stores the words, and then also performs the action requested.

https://github.com/saumyabahu/Travel-Safe/blob/master/MainActivity.java

Upvotes: 0

Abdul Waheed
Abdul Waheed

Reputation: 4678

If you only want to save one previously detected String, for this to achieve, you need to make a global String variable and store the value in that variable from results list.(Save the same String as you are setting on text view). But if you want to save all the strings, you need to make global String Arraylist and add all those string in that array list. Below is the code for that.

private EditText txtSpeechInput;
private ImageButton btnSpeak;
private final int REQ_CODE_SPEECH_INPUT = 100;
private List<String> previousStringList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    previousStringList = new ArrayList<>();

    txtSpeechInput = findViewById(R.id.txtSpeechInput);
    btnSpeak = (ImageButton) findViewById(R.id.btnSpeak);
    btnSpeak.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            promptSpeechInput();
        }
    });
}

private void promptSpeechInput() {
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
            getString(R.string.speech_prompt));
    intent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS, 20000000);

    try {
        startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
    } catch (ActivityNotFoundException a) {
        Toast.makeText(getApplicationContext(),
                getString(R.string.speech_not_supported),
                Toast.LENGTH_SHORT).show();
    }
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode) {
        case REQ_CODE_SPEECH_INPUT: {
            if (resultCode == RESULT_OK && null != data) {

                final ArrayList<String> result = data
                        .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);


                txtSpeechInput.setText(result.get(0));
                if (result.get(0) != null) {
                    previousStringList.add(result.get(0));
                }
            }
            break;
        }

    }
}

Hope that helps you.If you don't understand anything feel free to ask. If you don't want to save same String twice(already saved string), just replace below conditional line of code..

if (result.get(0) != null && !previousStringList.contains(result.get(0))) {
    previousStringList.add(result.get(0));
   }

Upvotes: 1

Related Questions