使用Android语音识别,只有按下按钮才能停止

时间:2013-06-17 09:29:52

标签: android speech-recognition voice-recognition

我想用语音识别来记录,直到按下停止按钮。 你知道怎么办?

我尝试了一种解决方案,但只有在你不休息太久(4-5秒)的情况下才有效。 如果在停止时按STOP按钮,则再次开始工作......

我该如何纠正?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myText = (TextView) findViewById(R.id.STT);

    intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    recognizer = SpeechRecognizer
            .createSpeechRecognizer(getApplicationContext());
    recognizer.setRecognitionListener(this);

}

public void start(View view) {
    recognizer.startListening(intent);
}

public void stop(View view) {
    recognizer.stopListening();
}

@Override
public void onResults(Bundle res) {

    ArrayList<String> results = res
            .getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);

    myText.setText(results.get(0));
    // ...

    recognizer.startListening(intent);

}

@Override
public void onRmsChanged(float rmsdB) {
    //...
}

@Override
public void onReadyForSpeech(Bundle params) {
    //...
}

@Override
public void onPartialResults(Bundle data) {
    //...
}

@Override
public void onEvent(int eventType, Bundle params) {
    //...
}

@Override
public void onError(int error) {
    //...
}

@Override
public void onEndOfSpeech() {
    //...
}

@Override
public void onBufferReceived(byte[] buffer) {
    //...
}

@Override
public void onBeginningOfSpeech() {
    //...
}

1 个答案:

答案 0 :(得分:1)

这可能是设计中的JB问题。对于解决方法,您可以在服务中实现语音识别,然后根据结果将更新发送到UI。有关服务工作的实现,请参阅Android Speech Recognition as a service on Android 4.1 & 4.2