我正在研究用户的音频并将其保存在arrayList中,然后搜索此数组列表以查看谷歌音频API生成的所需单词是否存在,以便我们可以执行操作。删除我想要的操作并用textview替换它只是为了查看它是否正常工作,音频识别是否正常工作,但我始终将textview文本作为停止。那么问题是什么?
package myfirstapp.myapps.me.voicerecognition;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends ActionBarActivity implements View.OnClickListener {
ListView lv;
Button recBtn;
static final int check = 1111;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.lv);
recBtn = (Button) findViewById(R.id.recBtn);
tv = (TextView) findViewById(R.id.textView);
recBtn.setOnClickListener(MainActivity.this);
}
public void onClick(View v) {
Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
i.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak");
startActivityForResult(i, check);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == check && resultCode == RESULT_OK) {
ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
lv.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, results));
String s = "start";
for (String string : results) {
// if(string.matches("start"))
// tv.setText("Ok");
// else
// tv.setText("No start");
// }
if (string.startsWith(s))
tv.setText("Start");
else
tv.setText("Stop");
}
super.onActivityResult(requestCode, resultCode, data);
}
}
}
答案 0 :(得分:2)
找到匹配项时不会从循环中断,所以除非匹配项位于列表的最后一个String中,否则文本视图将在末尾包含Stop。
您可能希望将其更改为:
boolean match = false;
for (String string : results) {
if (string.startsWith(s)) {
tv.setText("Start");
match = true;
break;
}
}
if (!match)
tv.setText("Stop");