Building an Android app with voice recognition and speech synthesis

In this tutorial, we will explore how to create an Android app that can both recognize voice input and synthesize speech output. This functionality can be useful in various applications, such as virtual assistants, accessibility tools, and language learning apps. We will use Android's built-in SpeechRecognizer and TextToSpeech classes to achieve this. Let's get started!

Prerequisites

Step 1: Create a New Android Studio Project

  1. Open Android Studio and create a new project.
  2. Select "Empty Activity" as the template and click "Next."
  3. Enter a name for your application, choose a suitable package name, and select the minimum SDK version (API 21: Android 5.0 is recommended).
  4. Click "Finish" to create the project.

Step 2: Add Permissions to the AndroidManifest.xml

Open the AndroidManifest.xml file and add the following permissions:

Step 3: Add UI Elements to activity_main.xml

Open the activity_main.xml file and add the following code:

This code adds a button for speech input, an EditText for displaying the recognized text, and a button for speech output.

Step 4: Initialize SpeechRecognizer and TextToSpeech

Open the MainActivity.java file and add the following import statements:

 import android.speech.RecognitionListener; import android.speech.RecognizerIntent; import android.speech.SpeechRecognizer; import android.speech.tts.TextToSpeech; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import java.util.ArrayList; import java.util.Locale;

Add the following variables and initialize them in the onCreate method:

 private SpeechRecognizer speechRecognizer; private TextToSpeech textToSpeech; private Button btnSpeak, btnListen; private EditText etInput; @Override protected void onCreate(Bundle savedInstanceState) < super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnSpeak = findViewById(R.id.btnSpeak); btnListen = findViewById(R.id.btnListen); etInput = findViewById(R.id.etInput); // Initialize SpeechRecognizer speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this); speechRecognizer.setRecognitionListener(new SpeechRecognitionListener()); // Initialize TextToSpeech textToSpeech = new TextToSpeech(this, status -> < if (status == TextToSpeech.SUCCESS) < int result = textToSpeech.setLanguage(Locale.US); if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) < Toast.makeText(MainActivity.this, "Language not supported", Toast.LENGTH_SHORT).show(); >> else < Toast.makeText(MainActivity.this, "Initialization failed", Toast.LENGTH_SHORT).show(); >>); // Set button click listeners btnSpeak.setOnClickListener(this::startListening); btnListen.setOnClickListener(this::speak); >

Step 5: Implement Speech Recognition and Synthesis Methods

Add the following methods to the MainActivity class:

 private void startListening(View view) < 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, "Speak now"); speechRecognizer.startListening(intent); >private void speak(View view) < String text = etInput.getText().toString(); textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null, null); >@Override protected void onDestroy() < if (speechRecognizer != null) < speechRecognizer.destroy(); >if (textToSpeech != null) < textToSpeech.stop(); textToSpeech.shutdown(); >super.onDestroy(); > private class SpeechRecognitionListener implements RecognitionListener < @Override public void onResults(Bundle results) < ArrayListmatches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); if (matches != null) < etInput.setText(matches.get(0)); >> // Implement other required methods with empty bodies @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 onPartialResults(Bundle partialResults) <> @Override public void onEvent(int eventType, Bundle params) <> >

Now, run your app on an Android device or emulator. You can speak to the app by pressing the "Speak" button, and it will recognize your voice input and display it in the EditText. Pressing the "Listen" button will make the app read the text aloud.

Conclusion

You have successfully created an Android app with voice recognition and speech synthesis capabilities. You can now build upon this foundation to create more advanced applications, such as virtual assistants, accessibility tools, and language learning apps. Remember, if you need help or want to collaborate with experts, consider hiring Android developers.