每次我想说话时,我如何使Android上的语音到文本应用程序连续工作(即;无需单击按钮)?我可以使用哪个事件?
谢谢,
您需要使用SpeechRecognizer
查看recognizeDirectly
此类中的方法:
https ://github.com/gast-lib/gast-lib/blob/master/library/src/root/gast/speech/SpeechRecognizingAndSpeakingActivity.java
警告:当前的 Android api 不能可靠地工作。它可能会连续记录一段时间然后崩溃。此外,它会在更高版本的 Android 中向您的用户发出哔哔声,这可能是不可取的。
公共类 MainActivity 扩展 Activity {
private static final int REQUEST_CODE = 1234;
private ListView resultList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
resultList = (ListView) findViewById(R.id.list);
// to check if recognizer available or not
PackageManager pm = getPackageManager();
List<ResolveInfo> activities = pm.queryIntentActivities(new Intent(
RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
if (activities.size() == 0) {
Toast.makeText(getApplicationContext(), "Recognizer Not Found",
1000).show();
}
startVoiceRecognitionActivity();
}
private void startVoiceRecognitionActivity() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
"AndroidBite Voice Recognition...");
intent.putExtra(
RecognizerIntent.EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS,
5000000);
startActivityForResult(intent, 1234);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
ArrayList<String> matches = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
System.out.println("Matches list " + matches);
resultList.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, matches));
}
super.onActivityResult(requestCode, resultCode, data);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
//它会解决你的问题
你需要写一个服务,它会在后台运行,你必须学习一个类RecognitionService和一个接口RecognitionListener 干杯