简体   繁体   中英

Speech to text API in android

I have a recorded voice, I got a task to convert that recorded voice into text without using internet.

How can I achieve this, I tried like below:

Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
        RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Voice recognition Demo...");
startActivityForResult(intent, REQUEST_CODE);

But I am not getting, how do I send recorded file and get the text as result.

The RecognizerIntent is only for the situation which does not suit you, live voice input with Internet connection.

There's an answer here which might help further.

There is one available RecognizerIntent in android.

Step 1: Starting RecognizerIntent First we need to create a RecognizerIntent by setting necessary flags such as ACTION_RECOGNIZE_SPEECH – Simply takes user's speech input and returns it to same activity LANGUAGE_MODEL_FREE_FORM – Considers input in free form English EXTRA_PROMPT – Text prompt to show to the user when asking them to speak

Step 2: Receiving the speech response Once your speech input is done,this intent return all the possible result in OnActivityResult.As usual we can consider the first result as most accurate and take possible action whatever we need to do.

Please refer following code snippet and link for complete reference.

LINK : http://www.androidhive.info/2014/07/android-speech-to-text-tutorial/

/**
* Showing google speech input dialog
* */
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));
try {
startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
        getString(R.string.speech_not_supported),
        Toast.LENGTH_SHORT).show();
}
}
/**
* Receiving speech input
* */
@Override
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) {
        ArrayList<String> result = data
                          .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
        txtSpeechInput.setText(result.get(0));
    }
    break;
}
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM