在Android上将语音转换为文本

时间:2016-07-03 07:23:28

标签: java android speech-recognition

我是Android新手,我必须做一个项目,我需要创建一个可以用来获取语音命令的应用程序。我需要录制用户的声音,将其保存为音频文件,然后转换为文本或直接将其转换为文本文件而不保存音频文件。

这是我迄今为止所做的,

public class VoiceRecognitionDemo extends Activity{

    private static final int REQUEST_CODE = 1234;
    private ListView wordsList;

    /**
     * Called with the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.voice_recog);

        Button speakButton = (Button) findViewById(R.id.speakButton);

        wordsList = (ListView) findViewById(R.id.list);

        // Disable button if no recognition service is present
        PackageManager pm = getPackageManager();
        List<ResolveInfo> activities = pm.queryIntentActivities(
                new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
        if (activities.size() == 0)
        {
            speakButton.setEnabled(false);
            speakButton.setText("Recognizer not present");
        }
    }

    /**
     * Handle the action of the button being clicked
     */
    public void speakButtonClicked(View v)
    {
        startVoiceRecognitionActivity();
    }

    /**
     * Fire an intent to start the voice recognition activity.
     */
    private void startVoiceRecognitionActivity()
    {
        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);
    }

    /**
     * Handle the results from the voice recognition activity.
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK)
        {
            // Populate the wordsList with the String values the recognition engine thought it heard
            ArrayList<String> matches = data.getStringArrayListExtra(
                    RecognizerIntent.EXTRA_RESULTS);
            wordsList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
                    matches));
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}

我需要对此数组进行排序以提取某些关键字以触发操作。如何触发某些操作(例如 - 通过短语呼叫发起呼叫,与之交谈)?

1 个答案:

答案 0 :(得分:1)

您可以使用Nuance SDK识别语音转文本。它是一个跨平台的SDK,支持40多种语言。此SDK不是免费的,但您可以使用30天的测试密钥。

您可以将音频文件或流发送到Nuance服务器。作为响应,您将获得一个文本字符串。然后,您可以实现自己获取关键字和命令的逻辑。