简体   繁体   English

语音到Android中的文本API

[英]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. RecognizerIntent仅适用于不适合您的情况,可以通过Internet连接进行实时语音输入。

There's an answer here which might help further. 有一个答案在这里可能进一步帮助。

There is one available RecognizerIntent in android. android中有一个可用的RecognizerIntent。

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 步骤1:启动RecognizerIntent首先,我们需要通过设置必要的标记(例如ACTION_RECOGNIZE_SPEECH)来创建RecognizerIntent-简单地接受用户的语音输入并将其返回到相同的活动LANGUAGE_MODEL_FREE_FORM-考虑以自由格式输入英语EXTRA_PROMPT-在询问时向用户显示文本提示他们说话

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. 第2步:接收语音响应一旦完成语音输入,此意图便会在OnActivityResult中返回所有可能的结果。通常,我们可以将第一个结果视为最准确,并采取任何可能的措施。

Please refer following code snippet and link for complete reference. 请参考以下代码段和链接以获取完整参考。

LINK : http://www.androidhive.info/2014/07/android-speech-to-text-tutorial/ 链接: 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;
}
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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