我有一个使用Speech to Text的项目,您可以在不打字的情况下发送短信并通过语音发送。 现在我有一个代码,用户说出他/她想要发送的单词/句子,我也有一个确认,用户需要说“是”才能发送。
现在我的代码是
//Voice recording message via mic
public void promptSpeechInput() {
//Create intent to recognize the speech and we have putExtra to have other values than voice itself
Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
i.putExtra(RecognizerIntent.EXTRA_PROMPT, "Say something!");
//StartActivityForResult always binded by intent, getting the result from previous intent
try {
startActivityForResult(i, 100);
} catch (ActivityNotFoundException e) {
Toast.makeText(this, "You device does not support LazySpeech App", Toast.LENGTH_LONG).show();
}
}
这是我的 onActivityResult 代码
@Override
protected void onActivityResult(int request_code, int result_code, Intent data) {
super.onActivityResult(request_code, result_code, data);
switch (request_code) {
case 100:
if (result_code == RESULT_OK && data != null) {
ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
sampleTextView.setText(result.get(0));
String getMessage = sampleTextView.getText().toString();
messageHolder(getMessage);
String getAnswer = sampleTextView2.getText().toString();
switch(getAnswer){
case"yes":
ttsobject.speak("yes?", TextToSpeech.QUEUE_FLUSH, null);
sampleTextView3.setText(getMessage);
break;
case"no":
ttsobject.speak("No?", TextToSpeech.QUEUE_FLUSH, null);
break;
default:
break;
}
}
}
}
这是我的确认文本
public String messageHolder(String getMessage){
if(getMessage != ""){
if (Build.VERSION.RELEASE.startsWith("15")) {
ttsobject.speak("You want to send this message".concat(getMessage) + "?", TextToSpeech.QUEUE_FLUSH, null, null);
activateMicButton();
} else {
ttsobject.speak("You want to send this message".concat(getMessage) + "?", TextToSpeech.QUEUE_FLUSH, null);
activateMicButton();
}
}else{
if (Build.VERSION.RELEASE.startsWith("15")) {
ttsobject.speak("say your words again", TextToSpeech.QUEUE_FLUSH, null, null);
activateMicButton();
} else {
ttsobject.speak("say your words again", TextToSpeech.QUEUE_FLUSH, null);
activateMicButton();
}
}
return getMessage;
}
例如,他/她说
&#34;嘿,我想吃晚饭?&#34;。
问题是,我怎样才能得到他/她想发送的字样(嘿想要吃晚餐),因为我能得到的只是&#34; YES&#34;或&#34;否&#34;我确认的文字。
我尝试过创建类似的东西
sampleTextView2.setText(result.get(1));
String getSentence = sampleTextView.getText().toString();
我也试过制作静态字符串,把它放在globalvariable,
仍然没有运气。每当我在麦克风上说些什么时,这些话就不断变化, 有谁知道如何解决这个问题?
答案 0 :(得分:0)
您可以通过复制字符串而不是指向它的指针来保存以前的单词,就像使用全局变量一样。您可以使用以下构造函数创建新的String
对象来完成此操作:
String theWords = new String(result.get(0));
然后您可以使用theWords
变量来获取用户想要发送的消息。