安卓系统上的语音转文本

我希望创建一个具有语音到文本的应用程序。

我知道这种使用识别器Intent的能力:http://android-developers.blogspot.com/search/label/Speech%20Input

但是 - 我不希望弹出新的 Intent,我想在我当前的应用程序中对某个点进行分析,并且我不希望它弹出一些内容,说明它当前正在尝试录制您的声音。

有没有人关于如何最好地做到这一点的任何想法。我可能正在考虑尝试狮身人面像4 - 但我不知道这是否能够在Android上运行 - 有人有任何建议或经验吗?!

我想知道我是否可以更改此处的代码,使其可能不费心显示UI或按钮,而只是进行处理:http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/VoiceRecognition.html

干杯


答案 1

如果不想使用 执行语音识别,仍可以使用 SpeechRecognizer 类来执行此操作。但是,使用该类比使用意图要棘手一些。作为最后一点,我强烈建议让用户知道他何时被记录,否则当他最终发现时,他可能会非常设置。RecognizerIntent

编辑:一个小例子启发(但改变了)从,语音识别器导致ANR...我需要有关安卓语音 API 的帮助

Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
        RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
        "com.domain.app");

SpeechRecognizer recognizer = SpeechRecognizer
        .createSpeechRecognizer(this.getApplicationContext());
RecognitionListener listener = new RecognitionListener() {
    @Override
    public void onResults(Bundle results) {
        ArrayList<String> voiceResults = results
                .getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
        if (voiceResults == null) {
            System.out.println("No voice results");
        } else {
            System.out.println("Printing matches: ");
            for (String match : voiceResults) {
                System.out.println(match);
            }
        }
    }

    @Override
    public void onReadyForSpeech(Bundle params) {
        System.out.println("Ready for speech");
    }

    /**
     *  ERROR_NETWORK_TIMEOUT = 1;
     *  ERROR_NETWORK = 2;
     *  ERROR_AUDIO = 3;
     *  ERROR_SERVER = 4;
     *  ERROR_CLIENT = 5;
     *  ERROR_SPEECH_TIMEOUT = 6;
     *  ERROR_NO_MATCH = 7;
     *  ERROR_RECOGNIZER_BUSY = 8;
     *  ERROR_INSUFFICIENT_PERMISSIONS = 9;
     *
     * @param error code is defined in SpeechRecognizer
     */
    @Override
    public void onError(int error) {
        System.err.println("Error listening for speech: " + error);
    }

    @Override
    public void onBeginningOfSpeech() {
        System.out.println("Speech starting");
    }

    @Override
    public void onBufferReceived(byte[] buffer) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onEndOfSpeech() {
        // TODO Auto-generated method stub

    }

    @Override
    public void onEvent(int eventType, Bundle params) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onPartialResults(Bundle partialResults) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onRmsChanged(float rmsdB) {
        // TODO Auto-generated method stub

    }
};
recognizer.setRecognitionListener(listener);
recognizer.startListening(intent);

重要提示:从 UI 线程运行此代码,并确保具有所需的权限。

<uses-permission android:name="android.permission.RECORD_AUDIO" />

答案 2

在您的活动中执行以下操作:

Image button buttonSpeak = findView....;// initialize it.
buttonSpeak.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));
    try {
        startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
    } catch (ActivityNotFoundException a) {
        Toast.makeText(getApplicationContext(),
                getString(R.string.speech_not_supported),
                Toast.LENGTH_SHORT).show();
    }
}

    @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) {

                result = data
                        .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

      EditText input ((EditText)findViewById(R.id.editTextTaskDescription));
      input.setText(result.get(0)); // set the input data to the editText alongside if want to.

            }
            break;
        }

    }
}

推荐