android语音机器人代码,Android studio开发小型对话机器人app(实例代码)
时间: 2024-03-10 10:18:20 浏览: 107
以下是一个简单的Android语音机器人应用的实例代码,使用Android Studio进行开发。
1. 在布局文件中添加一个TextView和一个Button控件,用于显示对话和启动语音识别:
```
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/dialogue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:padding="10dp" />
<Button
android:id="@+id/speech_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Speak"
android:layout_below="@id/dialogue"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
```
2. 创建一个名为MainActivity的活动,并在其中添加以下代码:
```
public class MainActivity extends AppCompatActivity {
private TextView dialogue;
private Button speechBtn;
private SpeechRecognizer speechRecognizer;
private Intent speechRecognizerIntent;
private String keeper = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dialogue = findViewById(R.id.dialogue);
speechBtn = findViewById(R.id.speech_btn);
speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
speechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
speechRecognizer.setRecognitionListener(new RecognitionListener() {
@Override
public void onReadyForSpeech(Bundle params) { }
@Override
public void onBeginningOfSpeech() { }
@Override
public void onRmsChanged(float rmsdB) { }
@Override
public void onBufferReceived(byte[] buffer) { }
@Override
public void onEndOfSpeech() { }
@Override
public void onError(int error) { }
@Override
public void onResults(Bundle results) {
ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
if(matches != null){
String text = matches.get(0);
dialogue.setText(text);
processText(text.toLowerCase());
}
}
@Override
public void onPartialResults(Bundle partialResults) { }
@Override
public void onEvent(int eventType, Bundle params) { }
});
speechBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
speechRecognizer.startListening(speechRecognizerIntent);
}
});
}
private void processText(String text) {
if(text.indexOf("hello") != -1 || text.indexOf("hi") != -1){
keeper = "Hello, how can I help you?";
speak();
}
else if(text.indexOf("what") != -1 && text.indexOf("name") != -1){
keeper = "My name is Chatbot";
speak();
}
else if(text.indexOf("how") != -1 && text.indexOf("you") != -1){
keeper = "I'm doing well, thank you. How about you?";
speak();
}
else if(text.indexOf("bye") != -1){
keeper = "Goodbye!";
speak();
finish();
}
}
private void speak(){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
TextToSpeech tts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if(status != TextToSpeech.ERROR){
tts.setLanguage(Locale.US);
tts.speak(keeper, TextToSpeech.QUEUE_FLUSH, null, null);
}
}
});
}
else {
Intent intent = new Intent();
intent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(intent, 1);
}
}
}
```
这个代码使用SpeechRecognizer来捕获用户的语音输入,并使用TextToSpeech来回答用户。它有一些简单的if/else语句来识别用户的输入并生成相应的回答。
请注意,这只是一个简单的示例应用程序,需要进一步扩展才能实现更复杂的对话。
阅读全文