我的应用程序使用Android的SpeechRecognizer类,并且几天前在几台设备上运行良好。在我的三星S3和我的LG-E980上,应用程序变得非常缓慢。我的其他设备不受影响。出现此问题是因为onBeginningOfSpeech和onEndOfSpeech之间有大约7秒的等待时间。 我已尝试在互联网上使用源代码的其他应用程序,结果相同。 是否有人知道可能导致此问题的最新软件更改? 我的三星S3运行Android 5.0.1运行良好的三星运行4.1.2
键盘麦克风没有延迟。
答案 0 :(得分:3)
我很确定这是因为谷歌应用程序(谷歌搜索)版本6减速。我在使用以下代码测试9台设备后发现了这一点:
package com.example.speechtest;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity implements RecognitionListener {
static String speechTestTag = "SpeechText";
MyView myView;
SpeechRecognizer speechRecognizer;
Intent speechIntent;
boolean processingSpeech = false;
long startTime;
long speechTime;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(BuildConfig.DEBUG) Log.d(speechTestTag, "onCreate");
myView = new MyView(this);
setContentView(myView);
speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
speechRecognizer.setRecognitionListener(this);
speechIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
speechIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
// speechIntent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS, 1100); // this line is ignored
// speechIntent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS, 0); // this line is ignored
}
class MyView extends LinearLayout {
TextView speechTimeView;
TextView textView;
Button talkButton;
public MyView(Context context) {
super(context);
setOrientation(LinearLayout.VERTICAL);
speechTimeView = new TextView(context);
speechTimeView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
textView = new TextView(context);
textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
talkButton = new Button(context);
talkButton.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
talkButton.setText("Touch to Talk");
talkButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(BuildConfig.DEBUG) Log.d(speechTestTag, "talkButton calling startListening - processingSpeech = " + processingSpeech);
talkButton.setText("Speak");
speechTimeView.setText(" "); //speechTimeView.invalidate();
textView.setText(" "); //textView.invalidate();
speechRecognizer.startListening(speechIntent);
}
});
addView(speechTimeView);
addView(textView);
addView(talkButton);
}
}
@Override
public void onReadyForSpeech(Bundle params) {
if(BuildConfig.DEBUG) Log.d(speechTestTag, "onReadyForSpeech");
processingSpeech = true;
}
@Override
public void onBeginningOfSpeech() {
if(BuildConfig.DEBUG) Log.d(speechTestTag, "onBeginningOfSpeech");
startTime = System.currentTimeMillis();
}
@Override
public void onRmsChanged(float rmsdB) {
//// if(BuildConfig.DEBUG) Log.d(speechTestTag, "onRmsChanged - new rmssB = " + rmsdB); // too much of this on some devices
}
@Override
public void onBufferReceived(byte[] buffer) {
if(BuildConfig.DEBUG) Log.d(speechTestTag, "onBufferReceived");
}
@Override
public void onEndOfSpeech() {
if(BuildConfig.DEBUG) Log.d(speechTestTag, "onEndOfSpeech");
long endTime = System.currentTimeMillis();
speechTime = endTime - startTime;
myView.speechTimeView.setText("SpeechTime: " + speechTime + " Milliseconds");
}
@Override
public void onError(int error) {
if(BuildConfig.DEBUG) Log.d(speechTestTag, "onError error: " + error + " processingSpeech = " + processingSpeech + " speechTime = " + speechTime);
if(error == 7 && (!processingSpeech || speechTime < 1700)) {
if(BuildConfig.DEBUG) Log.d(speechTestTag, "onError startListening");
speechRecognizer.startListening(speechIntent);
} else {
if(BuildConfig.DEBUG) Log.d(speechTestTag, "onError displaying error");
myView.textView.setText("< " + "ERROR " + error + " >");
myView.talkButton.setText("Touch to Talk");
}
processingSpeech = false;
}
@Override
public void onResults(Bundle results) {
ArrayList<String> recognitionResults = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
String myResult = recognitionResults.get(0);
if(BuildConfig.DEBUG) Log.d(speechTestTag, "onResults - <" + myResult + ">");
myView.textView.setText(myResult);
myView.talkButton.setText("Touch to Talk");
processingSpeech = false;
}
@Override
public void onPartialResults(Bundle partialResults) {
if(BuildConfig.DEBUG) Log.d(speechTestTag, "onPartialResults");
}
@Override
public void onEvent(int eventType, Bundle params) {
if(BuildConfig.DEBUG) Log.d(speechTestTag, "onEvent");
}
}
此代码是自包含的,不需要任何XML布局。
不要忘记添加
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
到您的清单。
此外,Google App的第3版支持离线语音识别。版本4没有。我没有版本5可以告诉。版本6.0.23.21确实支持离线,但速度非常慢。 问题是从onBeginningOfSpeech到onOndOfSpeech需要的时间。在语音停止后,它会一直等待大约5秒钟,然后才能确定不再有语音并继续进行识别(在线和离线)。即使您将其设为零或负数,也会忽略EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS参数。
我的三部手机都有谷歌应用程序版本6,但我能够恢复到以前的所有版本,问题就消失了。
ERROR 7还有另一个问题,我用boolean flag processingSpeech解决了这个问题。