我正在尝试编写一个应用程序,允许我说出命令并让应用程序执行我所说的。但问题是,我不想使用弹出 GoogleVoice 的 RecognizerIntent。我想拥有一个自己定制的。谁能给我一些帮助或提示,让我这样做?在我说了些什么并执行任务之后,也许对如何使用结果有一些帮助?
更新代码:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
public class VoicingMain extends Activity implements OnClickListener {
ListView lv;
private SpeechRecognizer sr;
private Intent srIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.voicing);
lv = (ListView) findViewById(R.id.lvList);
Button b = (Button) findViewById(R.id.bStartVoicing);
b.setOnClickListener(this);
boolean available = SpeechRecognizer.isRecognitionAvailable(this);
Log.d("Speech", "available = " + available);
sr = SpeechRecognizer.createSpeechRecognizer(this);
sr.setRecognitionListener(new SpeechListener());
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
srIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
srIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
srIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
this.getPackageName());
srIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 5);
Log.d("speech", "button active");
sr.startListening(srIntent);
new CountDownTimer(3000, 1000) {
@Override
public void onTick(long arg0) {
// TODO Auto-generated method stub
}
@Override
public void onFinish() {
// TODO Auto-generated method stub
sr.stopListening();
}
}.start();
}
private class SpeechListener implements RecognitionListener {
@Override
public void onBeginningOfSpeech() {
// TODO Auto-generated method stub
Log.d("Speech", "onBeginningOfSpeech");
}
@Override
public void onBufferReceived(byte[] arg0) {
// TODO Auto-generated method stub
Log.d("Speech", "onBufferReceived");
}
@Override
public void onEndOfSpeech() {
// TODO Auto-generated method stub
Log.d("Speech", "onEndOfSpeech");
}
@Override
public void onError(int arg0) {
// TODO Auto-generated method stub
Log.d("Speech", "onError");
}
@Override
public void onEvent(int arg0, Bundle arg1) {
// TODO Auto-generated method stub
Log.d("Speech", "onEvent");
}
@Override
public void onPartialResults(Bundle arg0) {
// TODO Auto-generated method stub
Log.d("Speech", "onPartialResults");
}
@Override
public void onReadyForSpeech(Bundle arg0) {
// TODO Auto-generated method stub
Log.d("Speech", "onReadyForSpeech");
}
@Override
public void onResults(Bundle arg0) {
// TODO Auto-generated method stub
Log.d("Speech", "results");
}
@Override
public void onRmsChanged(float arg0) {
// TODO Auto-generated method stub
// Log.d("Speech", "onRmsChanged");
}
}
}
这就是我到目前为止所拥有的