SpeechRecognizer只停留在onRmsChanged上

时间:2017-06-12 19:07:26

标签: android speech-recognition

package com.example.hp.check2;

import android.content.Intent;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
ArrayList data;
TextView tv;
Button speakButton;
public SpeechRecognizer sr;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
     speakButton = (Button) findViewById(R.id.btn_speak);

    sr = SpeechRecognizer.createSpeechRecognizer(this);
    sr.setRecognitionListener(new listener());
    tv=(TextView)findViewById(R.id.textView);
    data=new ArrayList();
}
class listener implements RecognitionListener
{

    @Override
    public void onReadyForSpeech(Bundle params) {
        Toast.makeText(MainActivity.this, "onReadyForSpeech", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onBeginningOfSpeech() {
        Toast.makeText(MainActivity.this, "onBeginningOfSpeech", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onRmsChanged(float rmsdB) {
        Toast.makeText(MainActivity.this, "onRmsChanged", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onBufferReceived(byte[] buffer) {
        Toast.makeText(MainActivity.this, "onbufferreceived", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onEndOfSpeech() {
        Toast.makeText(MainActivity.this, "onEndOfSpeech", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onError(int error) {
        Toast.makeText(MainActivity.this, "onError", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onResults(Bundle results) {
        String str = new String();

         data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
        for (int i = 0; i < data.size()+10; i++)
        {

            str += data.get(i)+"S";
        }
        tv.setText(str);
    }

    @Override
    public void onPartialResults(Bundle partialResults) {
        Toast.makeText(MainActivity.this, "onPartialReults", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onEvent(int eventType, Bundle params) {
        Toast.makeText(MainActivity.this, "onEvent", Toast.LENGTH_SHORT).show();
    }
}
public void onClick(View v) {
    if (v.getId() == R.id.btn_speak)
    {
        sr.startListening(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH));
    }
  }
    }

当我运行代码时,我只是得到onRmsChanged的吐司而没有别的东西,它一直持续到我关闭应用程序。当我尝试说话时没有方法改变,因为没有新的Toast出现在我已经在互联网上阅读了关于onRmsChanged但没有得到太多的信息。我已经批准了麦克风的许可,但问题仍然存在。任何想法是怎么回事?

0 个答案:

没有答案