Android 7.0上的SpeechRecognizer

时间:2018-04-27 18:26:25

标签: android version speech-to-text

我已经实现了SpeechRecognizer,它在Android 6上完美运行,但它不能用于android 7.0,我不知道为什么。

如果我尝试在startActivityforResult()上使用识别器,它可以正常工作,但如果我使用createListener方法,即使我尝试输出所有结果也没有任何反应。

这是我的代码:

class listener implements RecognitionListener
{
    String TAG = "AFFICHAGE";
    public void onReadyForSpeech(Bundle params)
    {
        Toast.makeText(MainActivity.this,"READY TO LISTEN", Toast.LENGTH_LONG).show();
    }
    public void onBeginningOfSpeech()
    {

    }
    public void onRmsChanged(float rmsdB)
    {

    }
    public void onBufferReceived(byte[] buffer)
    {
        Log.d(TAG, "onBufferReceived");
    }
    public void onEndOfSpeech()
    {
        Log.d(TAG, "onEndofSpeech");
    }
    public void onError(int error)
    {
        switch (error){
            case SpeechRecognizer.ERROR_AUDIO:
                Toast.makeText(MainActivity.this, "ERROR AUDIO",Toast.LENGTH_LONG).show();

                break;

            case SpeechRecognizer.ERROR_CLIENT:
                Toast.makeText(MainActivity.this, "ERROR CLIENT",Toast.LENGTH_LONG).show();

                break;

            case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS:
                Toast.makeText(MainActivity.this, "ERROR PERMISSION",Toast.LENGTH_LONG).show();

                break;

            case SpeechRecognizer.ERROR_NETWORK:
                Toast.makeText(MainActivity.this, "ERROR INTERNET",Toast.LENGTH_LONG).show();

                break;

            case SpeechRecognizer.ERROR_NETWORK_TIMEOUT:
                Toast.makeText(MainActivity.this, "ERROR TIMEOUT",Toast.LENGTH_LONG).show();

                break;

            case SpeechRecognizer.ERROR_NO_MATCH:
                Toast.makeText(MainActivity.this, "ERROR NO MATCH",Toast.LENGTH_LONG).show();

                break;

            case SpeechRecognizer.ERROR_RECOGNIZER_BUSY:
                Toast.makeText(MainActivity.this, "ERROR BUSY",Toast.LENGTH_LONG).show();

                break;

            case SpeechRecognizer.ERROR_SERVER:
                Toast.makeText(MainActivity.this, "ERROR SERVER",Toast.LENGTH_LONG).show();

                break;

            case SpeechRecognizer.ERROR_SPEECH_TIMEOUT:
                Toast.makeText(MainActivity.this, "ERROR TIMEOUT",Toast.LENGTH_LONG).show();

                break;

            default:

                break;
        }
    }
    public void onResults(Bundle results)
    {
        String str = new String();
        Log.d(TAG, "onResults " + results);
        ArrayList data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
        for (int i = 0; i < data.size(); i++)
        {
            Log.d(TAG, "result " + data.get(i));
            str += data.get(i);
        }
        txtSpeechInput.setText("results: "+String.valueOf(data.size()));
    }
    public void onPartialResults(Bundle partialResults)
    {
        Log.d(TAG, "onPartialResults");
    }
    public void onEvent(int eventType, Bundle params)
    {
        Log.d(TAG, "onEvent " + eventType);
    }
}

这就是我所说的:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    txtSpeechInput = (TextView) findViewById(R.id.txtSpeechInput);
    btn = (Button) findViewById(R.id.button);
    sr = SpeechRecognizer.createSpeechRecognizer(this);
    sr.setRecognitionListener(new listener());
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            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,"voice.recognition.test");

            //intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,5);
            sr.startListening(intent);
            Log.i("111111","11111111");
        }
    });
}

感谢回答。

1 个答案:

答案 0 :(得分:0)

我找到了一个解决方案,让它可以像这样初始化Google librairie:SpeechRecognizer.createSpeechRecognizer(getApplication(), ComponentName.unflattenFromString("com.google.android.googlequicksearchbox/com.google.android.voicesearch.serviceapi.GoogleRecognitionService")); 它的工作,所以如果它不是一个好的解决方案,我现在不会。