Hello Buddies.
Why not try something related to Android this time?
Like we always quote, a small piece of intro code can boost
up your confidence and make you familiar with technology you want to take head
on.
A basic program using “Android Studio”, “Speech API” and
your “Android Handset” can provide you with a cool app, you can boast around
with, and capture voice to try convert it into text.
The steps are very limited and simple –
1. Create a new project in Android Studio.
2.
Use your weapon – JAVA to write down a class
MainActivity.java
package
com.example.myfirstapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.util.ArrayList;
import java.util.Locale;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private final int SPEECH_RECOGNITION_CODE = 1;
private TextView txtOutput;
private ImageButton btnMicrophone;
private ImageButton btnMicrophone2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtOutput = (TextView) findViewById(R.id.txt_output);
btnMicrophone = (ImageButton) findViewById(R.id.btn_mic);
btnMicrophone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startSpeechToText();
}
});
}
private void startSpeechToText() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak something...");
try {
startActivityForResult(intent, SPEECH_RECOGNITION_CODE);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(), "Sorry! Speech recognition is not supported in this device.", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case SPEECH_RECOGNITION_CODE: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
String text = result.get(0);
txtOutput.setText(text);
}
break;
}
}
}
}
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.util.ArrayList;
import java.util.Locale;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private final int SPEECH_RECOGNITION_CODE = 1;
private TextView txtOutput;
private ImageButton btnMicrophone;
private ImageButton btnMicrophone2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtOutput = (TextView) findViewById(R.id.txt_output);
btnMicrophone = (ImageButton) findViewById(R.id.btn_mic);
btnMicrophone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startSpeechToText();
}
});
}
private void startSpeechToText() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak something...");
try {
startActivityForResult(intent, SPEECH_RECOGNITION_CODE);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(), "Sorry! Speech recognition is not supported in this device.", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case SPEECH_RECOGNITION_CODE: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
String text = result.get(0);
txtOutput.setText(text);
}
break;
}
}
}
}
3.
Make some changes in AndroidManifest.xml as
below –
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.myfirstapp" android:versionCode="1" android:versionName="1.0"> <application android:allowBackup="true" android:label="@string/app_name" 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>
4.
Under layout folder à Have activity_main.xml
as below –
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/black" android:orientation="vertical" > <TextView android:id="@+id/txt_output" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="50dp" android:textColor="@color/green" android:text="Talk Time !" android:textSize="50dp" android:textStyle="bold" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="200dp" android:gravity="center" android:orientation="vertical" > <ImageButton android:id="@+id/btn_mic" android:layout_width="100dp" android:layout_height="100dp" android:background="@color/black" android:scaleType="centerCrop" android:src="@mipmap/ic_launcher" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="English Speech to text using Google API" android:textColor="@color/white" android:textSize="15dp" android:textStyle="italic" /> </LinearLayout> </RelativeLayout>
5.
You can also add colors as per your choice in
values/colors.xml –
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="colorPrimary">#3F51B5</color> <color name="colorPrimaryDark">#303F9F</color> <color name="colorAccent">#FF4081</color> <color name="white">#FFFFFF</color> <color name="black">#000000</color> <color name="red">#FF0000</color> <color name="blue">#0000FF</color> <color name="green">#00FF00</color> <color name="yellow">#F0F000</color> </resources>
6.
Strings.xml –
<resources> <string name="app_name"> Speech To Text </string> </resources>
Once the compiler approves, you are ready to build your
project as APK.
Export the APK into your Android device and BOOM, you are
ready to install and flaunt you new Voice to Text converter.
The code mentioned above has been inspired from various hard-core
online materials available, leaving the theoretical and explanatory parts to
the readers. Happy Coding ...
No comments:
Post a Comment