android - 语音识别安卓应用

标签 android speech-recognition

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

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

最佳答案

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

此外,不要指望 SpeechRecognizer 在 onPartialResults() 内返回任何内容.它很少这样做。

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

这是您需要使用 SpeechRecognizer 的代码 fragment :

 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()
    {
    }

关于android - 语音识别安卓应用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9625045/

相关文章:

delphi - 可以在单词发音游戏中使用WIndows语音识别引擎吗?

iOS10语音识别 "Listening"音效

android - 在 Android/Cordova/Knockout 中使用数字输入捕获 "Enter"按键

android - 如何使用 Android 双簧管库处理扬声器更改?

android - MNC 使用 Google map 的权限问题

python - 在 Python 中使用 wav 文件录制音频和语音到文本的转换

Android 将语音语言更改为文本到日语不起作用

android - 如何将android camera2设置为比率18.5 :9

android - 底部导航图标不显示原始颜色

ios - iOS 是否提供内置的文本到语音支持或任何类似 NSSpeechRecognizer 的类?