【发布时间】:2015-01-01 12:31:09
【问题描述】:
我已经构建了一个使用语音识别的 java 应用程序,我创建了一个这样的字符串数组:
String[] greetings = {"hello", "hi", "yow"};
现在的问题是应用只检测数组“yow”的最后一个单词,而不是“hello”或“hi”
for (String strings: greetings)
{ if (mostLikelyThingHeard.contains(strings)) {
tts.speak("Hey nice to see you!",
TextToSpeech.QUEUE_FLUSH, null);
所以我真的不知道我做错了什么,也许mostLikelyThingHeard 也需要一个“for”循环?
完整代码:
package nl.giovanniterlingen.pws;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Main extends Activity implements OnInitListener {
private static final String TAG = "PWS";
private TextView result;
private TextToSpeech tts;
private Button speak;
private int SPEECH_REQUEST_CODE = 1234;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
speak = (Button) findViewById(R.id.bt_speak);
speak.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendRecognizeIntent();
}
});
speak.setEnabled(false);
result = (TextView) findViewById(R.id.tv_result);
tts = new TextToSpeech(this, this);
}
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
speak.setEnabled(true);
} else {
// failed to init
finish();
}
}
private void sendRecognizeIntent() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Aan het luisteren...");
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 100);
startActivityForResult(intent, SPEECH_REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == SPEECH_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
ArrayList<String> matches = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
if (matches.size() == 0) {
tts.speak("Ik heb niks gehoord, probeer het nog eens",
TextToSpeech.QUEUE_FLUSH, null);
} else {
String mostLikelyThingHeard = matches.get(0);
result.setText("Dit heeft u gezegd: "
+ mostLikelyThingHeard + ".");
String doei = "doei";
String[] greetings = { "hello", "hi", "yow" };
for (String strings : greetings) {
if (mostLikelyThingHeard.contains(strings)) {
tts.speak("Hey nice to see you!",
TextToSpeech.QUEUE_FLUSH, null);
} else if (mostLikelyThingHeard.equals(doei)) {
tts.speak("Okay tot de volgende keer!",
TextToSpeech.QUEUE_FLUSH, null);
} else {
tts.speak("Ik begrijp niet wat je bedoeld met "
+ mostLikelyThingHeard
+ " probeer het anders te verwoorden.",
TextToSpeech.QUEUE_FLUSH, null);
}
}
}
} else {
Log.d(TAG, "result NOT ok");
}
}
super.onActivityResult(requestCode, resultCode, data);
}
@Override
protected void onDestroy() {
if (tts != null) {
tts.shutdown();
}
super.onDestroy();
}
}
【问题讨论】:
-
您是否要同时说“你好”、“嗨”、“哟”?因为在这种情况下,循环将被执行三次,并且由于 TextToSpeech.QUEUE_FLUSH,之前的话语将被清除。
-
String[] greetings = { "hallo", "hi", "yow" };“你好”在哪里? -
好吧,你并没有在识别一个单词后打破你的循环。如果您说“hi”并且您的程序也识别“hi”,它仍然会检查其他单词。
-
打印你最有可能听到的东西?查看错误的确切位置
-
您知道如何在代码中设置断点并在调试模式下运行应用程序吗?
标签: java android arrays contains