1

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

我尝试了一个解决方案,但它只在您不休息太久(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() {
    //...
}
4

1 回答 1

1

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

于 2013-06-18T01:37:38.790 回答