Speech To Text | Android Studio | Java
Speech To Text
In this comprehensive tutorial, you'll learn how to implement Speech to Text functionality in an Android application using Java programming language in Android Studio. The tutorial covers step-by-step instructions, starting from setting up the project in Android Studio, integrating the necessary permissions, implementing the SpeechRecognizer API, handling runtime permissions, and displaying the recognized text on the user interface. Whether you're a beginner or an experienced Android developer, this tutorial will provide you with the necessary guidance to seamlessly integrate Speech to Text functionality into your Android application.
Code:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal" android:orientation="vertical" android:padding="20dp" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Speech To Text" android:textSize="20sp" /> <!--Button: on click start taking voice input--> <com.google.android.material.button.MaterialButton android:id="@+id/speakBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginVertical="20dp" android:drawablePadding="5dp" android:text="Voice Input" /> <!--TextView: set the spoken text--> <TextView android:id="@+id/spokenTextTv" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="" /> </LinearLayout>
MainActivity.java
package com.technifysoft.myapplication; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.speech.RecognizerIntent; import android.widget.TextView; import android.widget.Toast; import androidx.activity.result.ActivityResult; import androidx.activity.result.ActivityResultCallback; import androidx.activity.result.ActivityResultLauncher; import androidx.activity.result.contract.ActivityResultContracts; import androidx.appcompat.app.AppCompatActivity; import com.google.android.material.button.MaterialButton; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { private MaterialButton speakBtn; private TextView spokenTextTv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); speakBtn = findViewById(R.id.speakBtn); spokenTextTv = findViewById(R.id.spokenTextTv); speakBtn.setOnClickListener(view -> { //voice input voiceInput(); }); } private void voiceInput() { //language e.g. "en" for "English", "ur" for "Urdu", "hi" for "Hindi" etc. String language = "en"; Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, language); intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak to text"); try { voiceInputArl.launch(intent); } catch (Exception e) { Toast.makeText(this, " " + e.getMessage(), Toast.LENGTH_SHORT).show(); } } private final ActivityResultLauncher<Intent> voiceInputArl = registerForActivityResult( new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() { @Override public void onActivityResult(ActivityResult activityResult) { if (activityResult.getResultCode() == Activity.RESULT_OK) { ArrayList<String> result = activityResult.getData().getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); String text = result.get(0); spokenTextTv.append(text); } } } ); }
Comments
Post a Comment