3
我正在開發一個工作正常的自定義軟鍵盤。現在我想添加語音識別(STT)的功能,我使用的是android自定義api.But問題是這樣的當我按下麥克風按鈕時,出現語音識別器,我得到對應於語音的字符串。但是,如何將它再次發送到打印它的onkey事件中。我不知道。請幫助我。 下面是代碼:如何在我的自定義鍵盤中添加語音識別
public void onKey(int pc, int[] key) {
ic = getCurrentInputConnection();
al.clear();
//cv.clear();
switch (pc) {
case 45:
Speech.i=false;
Speech.ic=ic;
Intent intent = new Intent(getApplicationContext(),Speech.class);
intent.setFlags(intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
getCurrentInputConnection().commitText(Speech.spoken, 1);
}
這裏是語音類: -
public class Speech extends Activity {
static ArrayList<String> results;
static float[] confidence;
public static String spoken;
public static boolean i = true;
public static InputConnection ic;
@Override
public void onCreate(Bundle b) {
super.onCreate(b);
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
// Specify free form input
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
"or forever hold your peace");
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.ENGLISH);
startActivityForResult(intent, 5);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 5 && resultCode == RESULT_OK) {
results = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
String confidenceExtra = RecognizerIntent.EXTRA_CONFIDENCE_SCORES;
confidence = data.getFloatArrayExtra(confidenceExtra);
// TODO Do something with the recognized voice strings
}
spoken = null;
spoken=getWord();
Main.ic.commitText(spoken, 1);
//Main m =new Main();
//m.onKey(28, key)
//broadcastIntent();
finish();
}
public String getWord() {
float max = confidence[0];
int j = 0;
for (int i = 0; i < results.size(); i++) {
if (max < confidence[i]) {
j = i;
max = confidence[i];
}
}
//Log.i("main", "" + spoken);
i = true;
spoken=results.get(j);
Log.i("main", "" + spoken);
return spoken;
}
/* public void broadcastIntent()
{
Intent intent =new Intent();
intent.setAction("ttsdone");
sendBroadcast(intent);
}*/
}