How to Implement Speech Recognition in Android Apps

星河之舟 2021-08-12 ⋅ 22 阅读

Speech recognition is a powerful feature that can enhance the user experience in Android applications. With speech recognition, users can interact with the app using voice commands, making it more intuitive and accessible. In this blog post, we will explore how to implement speech recognition in Android apps using Kotlin and Java.

Setting up Speech Recognition

To implement speech recognition in Android apps, we first need to add the necessary permissions and dependencies to our project.

Permissions

Open the AndroidManifest.xml file and add the following permissions inside the <manifest> tag:

<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.INTERNET" />

The RECORD_AUDIO permission is required to access the device's microphone for capturing audio input, while the INTERNET permission is needed for any speech-to-text API calls.

Dependencies

Next, we need to add the necessary dependencies to our app's build.gradle file.

For Kotlin projects, add the following dependencies inside the dependencies block:

implementation 'androidx.core:core-ktx:1.6.0'
implementation 'androidx.activity:activity-ktx:1.4.0'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.4.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2'

For Java projects, use the following dependencies instead:

implementation 'androidx.core:core-ktx:1.6.0'
implementation 'androidx.activity:activity-ktx:1.4.0'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.4.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2'

These dependencies include libraries for handling Kotlin coroutines and AndroidX extensions, which will simplify our implementation.

Implementing Speech Recognition

Now let's dive into the code and implement speech recognition in our Android app.

First, create a new activity or fragment where you want to include speech recognition. Inside the layout XML file for the activity or fragment, add a button that the user will tap to start the speech recognition process:

<Button
    android:id="@+id/startSpeechRecognitionButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Start Speech Recognition" />

Next, in the corresponding Kotlin or Java file for the activity or fragment, initialize the speech recognition feature:

val speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this)
SpeechRecognizer speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);

Also, make sure to import the necessary classes for SpeechRecognizer and other related components.

Now, create a listener that will handle the results of the speech recognition process:

val speechRecognitionListener = object : RecognitionListener {
    override fun onReadyForSpeech(params: Bundle?) {
        // Called when the speech recognition engine is ready
    }

    override fun onBeginningOfSpeech() {
        // Called when the user starts speaking
    }

    override fun onEndOfSpeech() {
        // Called when the user finishes speaking
    }

    override fun onError(error: Int) {
        // Called when an error occurs during speech recognition
    }

    override fun onResults(results: Bundle?) {
        // Called when the speech recognition process successfully recognizes speech
    }

    // Implement other required methods here
}
RecognitionListener speechRecognitionListener = new RecognitionListener() {
    @Override
    public void onReadyForSpeech(Bundle params) {
        // Called when the speech recognition engine is ready
    }

    @Override
    public void onBeginningOfSpeech() {
        // Called when the user starts speaking
    }

    @Override
    public void onEndOfSpeech() {
        // Called when the user finishes speaking
    }

    @Override
    public void onError(int error) {
        // Called when an error occurs during speech recognition
    }

    @Override
    public void onResults(Bundle results) {
        // Called when the speech recognition process successfully recognizes speech
    }

    // Implement other required methods here
};

Next, set the listener for the speech recognizer:

speechRecognizer.setRecognitionListener(speechRecognitionListener)
speechRecognizer.setRecognitionListener(speechRecognitionListener);

Finally, add a click listener to the speech recognition button. Inside the click listener, start the speech recognition process:

startSpeechRecognitionButton.setOnClickListener {
    val intent = 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_MAX_RESULTS, 1)
    speechRecognizer.startListening(intent)
}
startSpeechRecognitionButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        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_MAX_RESULTS, 1);
        speechRecognizer.startListening(intent);
    }
});

In the code above, we create an Intent with the action ACTION_RECOGNIZE_SPEECH. We also specify the language model, language, and set the maximum number of results to be returned by the speech recognition engine.

Conclusion

Adding speech recognition to Android apps can greatly improve user interaction and accessibility. In this blog post, we learned how to implement speech recognition in Android apps using Kotlin or Java. By following the provided steps, you can easily integrate speech recognition into your Android applications and provide a more intuitive user experience.


全部评论: 0

    我有话说: