Friday, 4 July 2014

Speech To Text

speech to text conversion feature quite creatively. In today’s world of Siri, voice commands are of utmost importance. Android natively provides feature of Speech to Text so why not to use it in our app!

At the end of this post you can download the source code


Step 1: Create Basic Android Project

Go to New > Project > Android Project. Give the project name as SpeechToText


Step 2: Create an Layout



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Speach To Text Example"
        android:textAppearance="?android:attr/textAppearanceLarge" />
    <Button
        android:id="@+id/speakButton"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="30dp"
        android:text="Speak" />
    <TextView
        android:id="@+id/displayTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="30dp"
        android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>

Step 3: Android Java Code 


package com.example.activity;
import java.util.ArrayList;
import com.example.speechtotext.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class SpeechActivity extends Activity {
TextView displayText;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.speech_layout);

displayText = (TextView) findViewById(R.id.displayTextView);

Button speakButton = (Button) findViewById(R.id.speakButton);

speakButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

Intent in = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

in.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");

try {
startActivityForResult(in, 1);
} catch (Exception e) {
// TODO: handle exception
Toast.makeText(
getApplicationContext(),
"sorry, your device doesn't support speech to text feature",
Toast.LENGTH_LONG).show();
}

}
});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (requestCode == 1) {
if (resultCode == RESULT_OK && data != null) {
ArrayList<String> text = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

displayText.setText(text.get(0));
}
}
super.onActivityResult(requestCode, resultCode, data);
}

}

The Final Result looks like this


  

Download the source code from below link

DOWNLOAD NOW


No comments:

Post a Comment