我试图识别声音。它工作但几秒钟后它会自动断开并给出 rmsvalue -2.12 。如果我们再次开始重新协调,它会给我一个例外:
这是我的MainActivity.class,
public class MainActivity extends Activity {
private SpeechRecognizer mSpeechRecognizer;
private Intent mSpeechRecognizerIntent;
protected long mSpeechRecognizerStartListeningTime = 0;
TextView tvreconizetext;
Button speakButton;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
speakButton = (Button) findViewById(R.id.bSpeak);
tvreconizetext = (TextView) findViewById(R.id.tvreconizetext);
mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
mSpeechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS, 100000);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, this.getPackageName());
SpeechRecognitionListener listener = new SpeechRecognitionListener();
mSpeechRecognizer.setRecognitionListener(listener);
speakButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mSpeechRecognizer != null) {
mSpeechRecognizer.destroy();
}
}
protected class SpeechRecognitionListener implements RecognitionListener {
@Override
public void onBeginningOfSpeech() {
Log.e("tag", "onBeginningOfSpeech");
}
@Override
public void onBufferReceived(byte[] buffer) {
Log.e("tag", "onBufferReceived :" + buffer);
}
@Override
public void onEndOfSpeech() {
Log.e("tag", "onEndOfSpeech ");
}
@Override
public void onError(int error) {
if (mSpeechRecognizer != null) {
mSpeechRecognizer.cancel();
mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
} else {
mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
}
Log.e("tag", "onError :" + error);
}
@Override
public void onEvent(int eventType, Bundle params) {
Log.e("tag", "onEvent :" + eventType + "," + params);
}
@Override
public void onPartialResults(Bundle partialResults) {
Log.e("tag", "onPartialResults :" + partialResults);
}
@Override
public void onReadyForSpeech(Bundle params) {
Log.e("tag", "onReadyForSpeech :" + params);
}
@Override
public void onResults(Bundle results) {
ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
Log.e("matches", "matches ::" + matches.get(0));
tvreconizetext.setText(matches.get(0));
mSpeechRecognizer.cancel();
mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
}
@Override
public void onRmsChanged(float rmsdB) {
Log.e("tag", "onRmsChanged :" + rmsdB);
}
}
}
这是我的activity_main.xml
<Button
android:id="@+id/bSpeak"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Speak" />
<TextView
android:id="@+id/tvreconizetext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/bSpeak"
android:gravity="center"
android:textSize="18sp" />
我还使用了&#34; RECORD_AUDIO&#34; Manifest的许可。
这是例外
01-02 11:42:39.706:E / ChromiumNetwork(6713):上传时出现异常 方法01-02 11:42:39.706:E / ChromiumNetwork(6713): com.google.android.apps.gsa.shared.exception.GsaIOException:错误 代码:262182 01-02 11:42:39.706:E / ChromiumNetwork(6713):at com.google.android.apps.gsa.shared.io.y.abort(QueueDataSource.java:61) 01-02 11:42:39.706:E / ChromiumNetwork(6713):at com.google.android.apps.gsa.search.core.i.l.abort(EagerDataSource.java:94) 01-02 11:42:39.706:E / ChromiumNetwork(6713):at com.google.android.apps.gsa.search.core.i.a.a.e.a(MonitoredCronetRequest.java:169) 01-02 11:42:39.706:E / ChromiumNetwork(6713):at com.google.android.apps.gsa.search.core.i.a.a.e.a(MonitoredCronetRequest.java:44) 01-02 11:42:39.706:E / ChromiumNetwork(6713):at com.google.android.apps.gsa.search.core.i.a.a.e $ 2.A(MonitoredCronetRequest.java:271) 01-02 11:42:39.706:E / ChromiumNetwork(6713):at org.chromium.net.CronetUrlRequest $ 2.run(CronetUrlRequest.java:579) 01-02 11:42:39.706:E / ChromiumNetwork(6713):at com.google.android.apps.gsa.search.core.i.x $ 1.run(TaskRunnerScheduledExecutor.java:36) 01-02 11:42:39.706:E / ChromiumNetwork(6713):at java.util.concurrent.Executors $ RunnableAdapter.call(Executors.java:390) 01-02 11:42:39.706:E / ChromiumNetwork(6713):at java.util.concurrent.FutureTask.run(FutureTask.java:234)01-02 11:42:39.706:E / ChromiumNetwork(6713):at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 01-02 11:42:39.706:E / ChromiumNetwork(6713):at java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:573) 01-02 11:42:39.706:E / ChromiumNetwork(6713):at java.lang.Thread.run(Thread.java:838)01-02 11:42:39.706: E / ChromiumNetwork(6713):at com.google.android.apps.gsa.shared.util.concurrent.a.q $ 1.run(GsaThreadFactory.java:118)
如何通过识别听众顺利识别语音。如果有人有想法请分享。