我创建了一个简单的 android 应用程序来控制连接到我的 Raspberry Pi 的继电器。我使用按钮以及基本的语音识别来触发这些按钮并打开/关闭相应的中继通道。
到目前为止,语音识别部分由 RecognizerIntent 处理,其中我需要按下我的应用程序上的一个按钮来打开一个谷歌语音提示,它会听取我的语音命令并激活/停用控制继电器开关的相应按钮。
我想通过连续语音识别来做同样的事情,它允许应用程序连续收听我的命令,而无需用户按下应用程序上的按钮,从而实现免提操作。
这是我现有的代码,这是一种非常简单的语音识别方法,可以让我打开和关闭连接到继电器的各种设备的按钮:
public void micclick(View view) {
if(view.getId()==R.id.mic)
{promptSpeechInput();}
}
private void promptSpeechInput() {
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,"Speak!");
try{
startActivityForResult(i,100);
}
catch (ActivityNotFoundException a)
{
Toast.makeText(MainActivity.this,"Sorry your device doesn't support",Toast.LENGTH_SHORT).show();
}
}
public void onActivityResult(int requestCode, int resultCode, Intent i) {
super.onActivityResult(requestCode, resultCode, i);
String voicetxt;
switch (requestCode) {
case 100:
if (resultCode == RESULT_OK && i != null) {
ArrayList<String> result2 = i.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); …Run Code Online (Sandbox Code Playgroud)