Build a Voice Command Recognition System to Perform Operations in Android App Using Android Studio

Hello friend you may have many doubts about Build a Voice Command Recognition System to Perform Operations in Android App Using Android Studio We hope this article fills in all the doubts in the article. Let’s get back to the article.

Build a Voice Command Recognition System to Perform Operations in Android App Using Android Studio

Build a Voice Command Recognition System to Perform Operations in Android App Using Android Studio

java code

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;

import static android.Manifest.permission.RECORD_AUDIO;

public class MainActivity extends AppCompatActivity {

private SpeechRecognizer speechRecognizer;
private Intent intentRecognizer;
private TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ActivityCompat.requestPermissions(this, new String[]{RECORD_AUDIO}, PackageManager.PERMISSION_GRANTED);

textView = findViewById(R.id.textView);
intentRecognizer = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intentRecognizer.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);

speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
speechRecognizer.setRecognitionListener(new RecognitionListener() {
@Override
public void onReadyForSpeech(Bundle bundle) {

}

@Override
public void onBeginningOfSpeech() {

}

@Override
public void onRmsChanged(float v) {

}

@Override
public void onBufferReceived(byte[] bytes) {

}

@Override
public void onEndOfSpeech() {

}

@Override
public void onError(int i) {

}

@Override
public void onResults(Bundle bundle) {
ArrayList<String> matches = bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
String string = “”;
if(matches!=null){
string = matches.get(0);
textView.setText(string);
}

}

@Override
public void onPartialResults(Bundle bundle) {

}

@Override
public void onEvent(int i, Bundle bundle) {

}
});
}

public void StartButton(View view){
speechRecognizer.startListening(intentRecognizer);
}

public void StopButton(View view){

speechRecognizer.stopListening();
}
}

manifest file

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.example.myvoicecommandapp”>

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

<application
android:allowBackup=”true”
android:icon=”@mipmap/ic_launcher”
android:label=”@string/app_name”
android:roundIcon=”@mipmap/ic_launcher_round”
android:supportsRtl=”true”
android:theme=”@style/AppTheme”>
<activity android:name=”.MainActivity”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” />

<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
</application>

</manifest>

xml code

<?xml version=”1.0″ encoding=”utf-8″?>
<androidx.constraintlayout.widget.ConstraintLayout 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”
tools:context=”.MainActivity”>

<TextView
android:id=”@+id/textView”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Hello World!”
app:layout_constraintBottom_toBottomOf=”parent”
app:layout_constraintLeft_toLeftOf=”parent”
app:layout_constraintRight_toRightOf=”parent”
app:layout_constraintTop_toTopOf=”parent” />

<Button
android:id=”@+id/button”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_marginStart=”166dp”
android:layout_marginTop=”103dp”
android:onClick=”StartButton”
android:text=”@string/start”
app:layout_constraintStart_toStartOf=”parent”
app:layout_constraintTop_toTopOf=”parent” />

<Button
android:id=”@+id/button2″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_marginStart=”167dp”
android:layout_marginTop=”105dp”
android:onClick=”StopButton”
android:text=”@string/stop”
app:layout_constraintStart_toStartOf=”parent”
app:layout_constraintTop_toBottomOf=”@+id/textView” />

</androidx.constraintlayout.widget.ConstraintLayout>

Read Also: Build a PDF Document Generator Android App Using Android Studio and Java

Final Words

Build a Voice Command Recognition System to Perform Operations in Android App Using Android Studio the article will satisfy all your doubts.

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Deekshi. Here, I post about programming to help developers.

Share on:

Leave a Comment