0
Android開發新手,試圖將語音實現爲文本,將實時打印屏幕上的文字,但獲得以下錯誤。似乎無法理解問題出在哪裏。是因爲我從togglebutton事件中調用了startRecording()和stopRecording()還是完全是其他內容。試圖對文本實現語音,不知道出了什麼問題
com.example.android.moviebud E/SpeechRecognizer:未連接到識別服務
package com.example.android.moviebud;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.support.v7.app.AppCompatActivity;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
public class MainActivity extends AppCompatActivity implements RecognitionListener {
ToggleButton recBtn;
SpeechRecognizer recognizer;
Intent recognitionIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
recognizer = SpeechRecognizer.createSpeechRecognizer(this);
recognizer.setRecognitionListener(this);
recognitionIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recBtn = (ToggleButton) findViewById(R.id.recBtn);
recBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if (isChecked) {
recognizer.startListening(recognitionIntent);
} else {
recognizer.stopListening();
recognizer.destroy();
}
}
});
}
@Override
public void onReadyForSpeech(Bundle bundle) {
Toast.makeText(getApplicationContext(), "READY", Toast.LENGTH_SHORT).show();
}
@Override
public void onBeginningOfSpeech() {
Toast.makeText(getApplicationContext(), "Speech recognition started", Toast.LENGTH_SHORT).show();
}
@Override
public void onRmsChanged(float v) {
}
@Override
public void onBufferReceived(byte[] bytes) {
}
@Override
public void onEndOfSpeech() {
}
@Override
public void onError(int i) {
Toast.makeText(getApplicationContext(), "Some error occurred", Toast.LENGTH_SHORT).show();
}
@Override
public void onResults(Bundle bundle) {
TextView v = (TextView) findViewById(R.id.speech);
StringBuilder sb = new StringBuilder("");
for (String s : bundle.getStringArrayList(recognizer.RESULTS_RECOGNITION)) {
sb.append(s);
}
v.setText(sb.toString());
}
@Override
public void onPartialResults(Bundle bundle) {
}
@Override**strong text**
public void onEvent(int i, Bundle bundle) {
}
}