语音识别Android App

时间:2012-03-08 21:15:30

标签: android speech-recognition

我正在制作一个应用程序,它接收来自用户的命令并实时编写。什么是我最好的选择?像sphinx这样的第三方软件还是我应该使用内置的(android语音识别)?

其次我想让它实时写,就像我说它开始写作一样?

2 个答案:

答案 0 :(得分:6)

您应该使用内置的Android语音识别功能。具体来说,您需要操作SpeechRecognier API,以便没有弹出对话框。

另外,不要指望SpeechRecognizer在onPartialResults()内返回任何内容。它很少。

您可以尝试使用Sphinx,但似乎其他开发人员难以让它在Android上运行。也就是说,如果您希望自己的应用在没有互联网连接的情况下运行,那么sphinx将是您唯一的选择。

以下是使用SpeechRecognizer所需的代码片段:

 public void recognizeDirectly(Intent recognizerIntent)
    {
        // SpeechRecognizer requires EXTRA_CALLING_PACKAGE, so add if it's not
        // here
        if (!recognizerIntent.hasExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE))
        {
            recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
                    "com.dummy");
        }
        SpeechRecognizer recognizer = getSpeechRecognizer();
        recognizer.startListening(recognizerIntent);
    }

    @Override
    public void onResults(Bundle results)
    {
        Log.d(TAG, "full results");
        receiveResults(results);
    }

    @Override
    public void onPartialResults(Bundle partialResults)
    {
        Log.d(TAG, "partial results");
        receiveResults(partialResults);
    }

    /**
     * common method to process any results bundle from {@link SpeechRecognizer}
     */
    private void receiveResults(Bundle results)
    {
        if ((results != null)
                && results.containsKey(SpeechRecognizer.RESULTS_RECOGNITION))
        {
            List<String> heard =
                    results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
            float[] scores =
                    results.getFloatArray(SpeechRecognizer.CONFIDENCE_SCORES);
            receiveWhatWasHeard(heard, scores);
        }
    }

    @Override
    public void onError(int errorCode)
    {
        recognitionFailure(errorCode);
    }

    /**
     * stop the speech recognizer
     */
    @Override
    protected void onPause()
    {
        if (getSpeechRecognizer() != null)
        {
            getSpeechRecognizer().stopListening();
            getSpeechRecognizer().cancel();
            getSpeechRecognizer().destroy();
        }
        super.onPause();
    }

    /**
     * lazy initialize the speech recognizer
     */
    private SpeechRecognizer getSpeechRecognizer()
    {
        if (recognizer == null)
        {
            recognizer = SpeechRecognizer.createSpeechRecognizer(this);
            recognizer.setRecognitionListener(this);
        }
        return recognizer;
    }

    // other unused methods from RecognitionListener...

    @Override
    public void onReadyForSpeech(Bundle params)
    {
        Log.d(TAG, "ready for speech " + params);
    }

    @Override
    public void onEndOfSpeech()
    {
    }

答案 1 :(得分:4)

gregm是对的,但问题的主要“实时写入”部分没有得到回答。您需要添加一个额外的内容,以表明您有兴趣获得部分结果:

添加额外的意图对我有用

intent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);

警告:Partial不会返回新内容,也会返回前一个内容。所以你需要自己检查差异......