java - 没有超时的Android语音识别?

标签 java android speech-recognition voice-recognition

我正在使用 android 语音识别并编写了一些代码来识别所说的话。请看下面的代码。

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MainActivity extends Activity {

    private TextView returnedText;
    private ToggleButton toggleButton;
    private ProgressBar progressBar;
    private SpeechRecognizer speech = null;
    private Intent recognizerIntent;
    private ListView wordList;
    private String LOG_TAG = "VoiceRecognitionActivity";
    private List<String> previousInterim;
    private diff_match_patch diff;
    private String display = "test";

    private List<String>adapterList = new ArrayList<String>();
    ArrayAdapter<String>  adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        returnedText = (TextView) findViewById(R.id.textView1);
        progressBar = (ProgressBar) findViewById(R.id.progressBar1);
        toggleButton = (ToggleButton) findViewById(R.id.toggleButton1);
        wordList = (ListView)findViewById(R.id.word_list);

        adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_checked, adapterList);
        adapter.setNotifyOnChange(true);
        wordList.setAdapter(adapter);
        progressBar.setVisibility(View.INVISIBLE);

       // createRecog();
        speech = SpeechRecognizer.createSpeechRecognizer(getApplicationContext());
        toggleButton.setOnCheckedChangeListener(new ButtonListener());



    }

    private class ButtonListener implements OnCheckedChangeListener, Runnable
    {
        boolean isChecked;

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            this.isChecked = isChecked;
            this.run();
        }

        @Override
        public void run() {

            if (isChecked) {
                speech.stopListening();
                speech.cancel();
                speech.destroy();
                createRecog();
                progressBar.setVisibility(View.VISIBLE);
                progressBar.setIndeterminate(true);
                speech.startListening(recognizerIntent);
                adapter.clear();

                returnedText.setText("");

            } else {
                progressBar.setIndeterminate(false);
                progressBar.setVisibility(View.INVISIBLE);
                speech.stopListening();

            }


        }
    }

    private class RecognitionListenerClass implements RecognitionListener
    {


        @Override
        public void onBeginningOfSpeech() {
            Log.i(LOG_TAG, "onBeginningOfSpeech");
            progressBar.setIndeterminate(false);
            progressBar.setMax(10);
            wordList.computeScroll();
        }

        @Override
        public void onBufferReceived(byte[] buffer) {
            Log.i(LOG_TAG, "onBufferReceived: " + buffer);
        }

        @Override
        public void onEndOfSpeech() {
            Log.i(LOG_TAG, "onEndOfSpeech");
            progressBar.setIndeterminate(true);
            toggleButton.setChecked(false);
        }

        @Override
        public void onError(int errorCode) {
            String errorMessage = getErrorText(errorCode);
            Log.d(LOG_TAG, "FAILED " + errorMessage);
            returnedText.setText(errorMessage);
            toggleButton.setChecked(false);


          //  speech = null;
            toggleButton.performClick();
        }

        @Override
        public void onEvent(int arg0, Bundle arg1) {
            Log.i(LOG_TAG, "onEvent");
        }

        @Override
        public void onPartialResults(Bundle arg0) {
            Log.i(LOG_TAG, "onPartialResults");
            final ArrayList<String> matches = arg0.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
            final float[] scores = arg0.getFloatArray(SpeechRecognizer.CONFIDENCE_SCORES);
            receiveWhatWasHeard(matches, scores);

        }

        @Override
        public void onReadyForSpeech(Bundle arg0) {
            Log.i(LOG_TAG, "onReadyForSpeech");
        }

        @Override
        public void onResults(Bundle results) {

        }

        @Override
        public void onRmsChanged(float rmsdB) {
            Log.i(LOG_TAG, "onRmsChanged: " + rmsdB);
            progressBar.setProgress((int) rmsdB);
        }
    }


    private void receiveWhatWasHeard(ArrayList<String> matches, float[] scores) {

        Log.i(LOG_TAG, matches.get(0));
    returnedText.setText(matches.get(0));        
    }

    private void createRecog()
    {
        speech = SpeechRecognizer.createSpeechRecognizer(this);
        speech.setRecognitionListener(new RecognitionListenerClass());
        recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE,
                "en");
        recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
                this.getPackageName());
        recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
        recognizerIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3);
        recognizerIntent.putExtra("android.speech.extra.DICTATION_MODE", true);
        recognizerIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }



    public  String getErrorText(int errorCode) {
        String message;
        switch (errorCode) {
            case SpeechRecognizer.ERROR_AUDIO:
                message = "Audio recording error";
                break;
            case SpeechRecognizer.ERROR_CLIENT:
                message = "Client side error";
                break;
            case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS:
                message = "Insufficient permissions";
                break;
            case SpeechRecognizer.ERROR_NETWORK:
                message = "Network error";
                break;
            case SpeechRecognizer.ERROR_NETWORK_TIMEOUT:
                message = "Network timeout";
                break;
            case SpeechRecognizer.ERROR_NO_MATCH:
                message = "No match";
                break;
            case SpeechRecognizer.ERROR_RECOGNIZER_BUSY:
                message = "RecognitionService busy";
                break;
            case SpeechRecognizer.ERROR_SERVER:
                message = "error from server";
                break;
            case SpeechRecognizer.ERROR_SPEECH_TIMEOUT:
                message = "No speech input";
                break;
            default:
                message = "Didn't understand, please try again.";
                break;
        }

        return message;
    }

}

但是我需要连续运行它,但我一直没能做到。在语音结束时重新启动识别可能是个好主意,但它会发出很小的噪音,并且需要 1-3 秒才能加载,这意味着,一些口语单词将会丢失。

是的,我知道谷歌已经提到他们的系统不是为了连续识别;但是之前有几个人做过解决方法。不幸的是,这些变通办法现在似乎不起作用,因为它们是用非常古老的 API 编写的。如果旧版本有可能,那么新版本当然更有可能。

那么,关于如何实现这个连续识别任务有什么想法吗?我正在使用 API 15。

最佳答案

阅读here背景

this demo是连续的,并使用 pultz 博客评论流中提到的 chromium 源

全双工谷歌 API 工作正常 in android . AFAIK,它仍然受到速率限制,因此在生产应用程序中毫无用处。

谷歌全双工 stdout sample使用 curl cli

IBM watson 拥有支持连续模式的全双工和生产就绪 API。您将不得不深入了解文档详细信息,但一般的 cli 示例是 here

参见 continuous here在沃森 API 上

关于java - 没有超时的Android语音识别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34253862/

相关文章:

java - 无法启动 Internet Explorer ---Selenium

java - hibernate +OSIV。 2 笔交易。可重复读取

php - 确定用于 PHP DOTNET 的程序集名称

swift - 在 SpeechKit 框架中连续收听用户语音并检测语音结束静音

android - 使用蓝牙连接的连续 Android 语音识别

java - h :selectOneRadio containing images

java - 使用 Java、Dozer 和 Hibernate 将 DTO 映射和更新到数据库

android - 单独/小组开发人员如何在多个设备上进行测试?

java - 如何在 Java 中使用 Google S2 库创建多边形

android - android中mysql数据库的使用方法