好的,我尝试了每个网站中的每一个问题,谷歌甚至我尝试了每一个例子 为什么我不能用谷歌语音输入实时发短信?当我们点击键盘上的麦克风但是没有键码可以在自定义按钮中使用它来解决它是否有任何方法可以对文本进行实时语音或实现麦克风按钮(键码)时,它是否有效?
private EditText log;
private SpeechRecognizer sr;
private static final String TAG = "spk2txtD2";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button speakButton = (Button) findViewById(R.id.btn);
log = (EditText) findViewById(R.id.txt);
speakButton.setOnClickListener(this);
//get the SpeechRecognizer and set a listener for it.
sr = SpeechRecognizer.createSpeechRecognizer(this);
sr.setRecognitionListener(new listener());
}
@Override
public void finish() {
sr.destroy();
sr = null;
super.finish();
}
/*
* The Recognitionlistener for the SpeechRecognizer.
*/
class listener implements RecognitionListener {
public void onReadyForSpeech(Bundle params) {
Log.d(TAG, "onReadyForSpeech");
}
public void onBeginningOfSpeech(){
Log.d(TAG, "onBeginningOfSpeech");
}
public void onRmsChanged(float rmsdB){
Log.d(TAG, "onRmsChanged" );
}
public void onBufferReceived(byte[] buffer) {
Log.d(TAG, "onBufferReceived");
}
public void onEndOfSpeech() {
Log.d(TAG, "onEndofSpeech");
}
public void onError(int error) {
Log.d(TAG, "error " + error);
logthis("error " + error);
}
public void onResults(Bundle results) {
Log.d(TAG, "onResults " + results);
// Fill the list view with the strings the recognizer thought it could have heard, there should be 5, based on the call
ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
//display results.
log.setText(String.valueOf(matches));
logthis("results: "+String.valueOf(matches.size()));
for (int i = 0; i < matches.size(); i++) {
Log.d(TAG, "result " + matches.get(i));
logthis("result " +i+":"+ matches.get(i));
}
}
public void onPartialResults(Bundle partialResults)
{
Log.d(TAG, "onPartialResults");
}
public void onEvent(int eventType, Bundle params)
{
Log.d(TAG, "onEvent " + eventType);
}
}
public void onClick(View v) {
if (v.getId() == R.id.btn) {
//get the recognize intent
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
//Specify the calling package to identify your application
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,getClass().getPackage().getName());
//Given an hint to the recognizer about what the user is going to say
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
//specify the max number of results
// intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,0);
intent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS,true);
// intent.putExtra("android.speech.extra.DICTATION_MODE", true);
//User of SpeechRecognizer to "send" the intent.
sr.startListening(intent);
Log.i(TAG,"Intent sent");
}
}
/*
* simple method to add the log TextView.
*/
public void logthis (String newinfo) {
if (newinfo != "") {
log.setText(log.getText() + "\n" + newinfo);
}
}
}