Simple voice recognition application using Android Studio

Simple voice recognition application using Android Studio

Howdy viewers, today we are going to learn How to make a Simple voice recognition application using Android Studio in a easiest way. In this voice recognition tutorial I am going to share about the basic idea how you can make your Android device capture your speech but remember it will not act as you give command. If you are very fresh to Android, then I suggest to check my article Dreamed of Android Developer!

The Thinking!

Well, now I suppose, you might be started thinking this application needs a fully programming skills as it deals with recognition hardware. But, let me tell you, with Android Studio it makes very simpler to code. And also, you don’t need to worry about any recognition software or package because Google Recognition will come into picture which is inbuilt in all the Android Devices.

CREATE ANDROID STUDIO PROJECT:

First create an Android Studio project > New > Enter Project name (I will name it as LOLO Personal Assistant) > Select the OS level (version) and choose Phone > Blank Activity > Leave the MainActivity and activity-main.xml > Wait till the project gradle finishes.

Let’s enjoy coding!!!!

  • The very first thing you’re going to do is register the INTERNET in the AndroidManifest.xml file because the recognition will need internet connection to act.

AndroidManifest.xml:

Write a simple line of code <uses-permission android:name=”android.permission.INTERNET”></uses-permission> in the xml file.

==========================
<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.example.raktim.lolopersonalassistant”>
<uses-permission android:name=”android.permission.INTERNET”></uses-permission>
<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>

  • The second thing is to do is to design the layout of the application. In this tutorial I am going to need two TextView’s and one ImageButton where the button in the image is the picture downloaded and pasted in the drawable folder of the project in the format of png. The first TextView will give you the output of the speech and hence we gave it a ID of textSpeechInput. While the other TextView is just the static text.



activity_main.xml
——————
<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout 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:orientation=”vertical”
tools:context=”com.example.raktim.lolopersonalassistant.MainActivity”>
<TextView
android:id=”@+id/textSpeechInput”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentTop=”true”
android:layout_centerHorizontal=”true”
android:textColor=”@color/colorPrimary”
android:textSize=”26dp”
android:textStyle=”normal”
/>
<LinearLayout
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentBottom=”true”
android:layout_centerHorizontal=”true”
android:layout_marginBottom=”60dp”
android:gravity=”center”
android:orientation=”vertical”
>
<ImageButton
android:id=”@+id/btnSpeak”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:background=”@null”
android:src=”@mipmap/mic”
/>
<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_marginTop=”10dp”
android:text=”Tap on MIC to speak”
android:textColor=”@color/colorPrimary”
android:textSize=”15dp”
android:textStyle=”normal”
/>
</LinearLayout>
</RelativeLayout>

  • The third is to work with the controller MainActivity.java where we write all the java codes which will make the application to work accordingly.

MainActivity.java
———————
package com.example.raktim.lolopersonalassistant;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.speech.RecognizerIntent;
import android.speech.tts.TextToSpeech;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
import java.util.ArrayList;
import java.util.Locale;
public class MainActivity extends Activity {
private TextView txtSpeechInput;
private ImageButton btnSpeak;
private final int REQ_CODE_SPEECH_INPUT= 100;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSpeak = (ImageButton) findViewById(R.id.btnSpeak);
btnSpeak.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
promptSpeechText();
}
});
}
////Google Speech Recognition
private void promptSpeechText() {
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, “Say Something”);
try {
startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(), “Speech Recognition Not Supported”, Toast.LENGTH_LONG).show();
}
}
// //Receiving the voice input
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode,resultCode, data);
switch (requestCode) {
case REQ_CODE_SPEECH_INPUT: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
txtSpeechInput.setText(result.get(0));
}
break;
}
}
}
}

OUTPUT of the LOLO Personal Assistant:

1 Simple voice recognition application using Android Studio 1

2 Simple voice recognition application using Android Studio

3 Simple voice recognition application using Android Studio 2

Conclusion:

The application will not work on Emulator but you must try it in your real android device. Once, you success in capturing the voice input, you can go for the command.

Please feel free to copy and paste the code, if you find any difficulties or if I missed something then do let me know in the comment.

Leave a Reply

Your email address will not be published. Required fields are marked *