我如何使它成为提示,与用户对话



我正在构建一个应用程序,该应用程序需要Android Studio中的语音识别,我希望用户和应用程序之间有更多的交互。目前,应用程序可以通过告诉应用程序用户想去哪里来将用户带到另一个活动,但我希望能有更多的沟通,因此应用程序会发出语音,询问用户"我如何帮助你",而不是用文字写出来。

这可能吗?我是一个新手,所以一个好的解释将不胜感激。提前谢谢。

活动-"主屏幕">

public class homescreen extends AppCompatActivity {

public static final int REQ_CODE_SPEECH_INPUT = 100;
private TextView mVoiceInputTv;
private ImageView imgSpeech;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_homescreen);
mVoiceInputTv = (TextView) findViewById(R.id.mtext);
imgSpeech = (ImageView) findViewById(R.id.imgSpeech);
imgSpeech.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startVoiceInput();
}
});
}
private void startVoiceInput() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Hello, how can i assist you?");
try {
startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
} catch (ActivityNotFoundException a) {
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQ_CODE_SPEECH_INPUT: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
mVoiceInputTv.setText(result.get(0));
}
if(mVoiceInputTv.getText().toString().equals("activity")) {
Intent intent = new Intent(getApplicationContext(),quizactivity.class);
startActivity(intent);
}
if(mVoiceInputTv.getText().toString().equals("logout")) {
Intent intent = new Intent(getApplicationContext(),Settings.class);
startActivity(intent);
}
break;
}
}
}

}

有人在这里发布了TextToSpeech库的演示-声音(以及声音的好坏(取决于用户的设备和安装的内容。

一旦你初始化了一个实例,你就可以使用speak方法告诉它该说什么,这看起来相当无痛!当你有多件事想让它说时,它可能会变得有点棘手,你需要让它打断自己,或者把一件事说成下一行,诸如此类的逻辑。但实际的发言相当直截了当!

最新更新