Thursday, January 3, 2013

Android Speech To Text

make a project with name SpeechToTextDemo and a main activity with the name MainActivity and copy the following code to it


import java.util.ArrayList;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

protected static final int RESULT_SPEECH = 1;

private ImageButton btnSpeak;
private TextView txtText;

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

txtText = (TextView) findViewById(R.id.txtText);

btnSpeak = (ImageButton) findViewById(R.id.btnSpeak);

btnSpeak.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

Intent intent = new Intent(
RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

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

try {
startActivityForResult(intent, RESULT_SPEECH);
txtText.setText("i love you");
} catch (ActivityNotFoundException a) {
Toast t = Toast.makeText(getApplicationContext(),
"Ops! Your device doesn't support Speech to Text",
Toast.LENGTH_SHORT);
t.show();
}
}
});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

switch (requestCode) {
case RESULT_SPEECH: {
if (resultCode == RESULT_OK && null != data) {

ArrayList<String> text = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

txtText.setText(text.get(0));
}
break;
}

}
}
}

activity_main.xml is the layout where you need to copy the following layout to it

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/textView1"
    android:layout_toLeftOf="@+id/textView1"
    android:gravity="center"
    android:orientation="vertical" >

    <ImageButton
        android:id="@+id/btnSpeak"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:contentDescription="@string/speak"
        android:src="@android:drawable/ic_btn_speak_now" />

    <TextView
        android:id="@+id/txtText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

6 comments:

  1. its getting forcefully closed.

    ReplyDelete
  2. Hi if it is than i will try to send you the code file, i tested before posting, please tell me where the exception is coming?

    ReplyDelete
  3. its logcat is here

    04-23 13:03:22.957: E/AndroidRuntime(7440): FATAL EXCEPTION: main
    04-23 13:03:22.957: E/AndroidRuntime(7440): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.speechtotextdemo/com.example.speechtotextdemo.MainActivity}: java.lang.NullPointerException
    04-23 13:03:22.957: E/AndroidRuntime(7440): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
    04-23 13:03:22.957: E/AndroidRuntime(7440): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
    04-23 13:03:22.957: E/AndroidRuntime(7440): at android.app.ActivityThread.access$600(ActivityThread.java:141)
    04-23 13:03:22.957: E/AndroidRuntime(7440): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
    04-23 13:03:22.957: E/AndroidRuntime(7440): at android.os.Handler.dispatchMessage(Handler.java:99)
    04-23 13:03:22.957: E/AndroidRuntime(7440): at android.os.Looper.loop(Looper.java:137)
    04-23 13:03:22.957: E/AndroidRuntime(7440): at android.app.ActivityThread.main(ActivityThread.java:5039)
    04-23 13:03:22.957: E/AndroidRuntime(7440): at java.lang.reflect.Method.invokeNative(Native Method)
    04-23 13:03:22.957: E/AndroidRuntime(7440): at java.lang.reflect.Method.invoke(Method.java:511)
    04-23 13:03:22.957: E/AndroidRuntime(7440): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    04-23 13:03:22.957: E/AndroidRuntime(7440): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    04-23 13:03:22.957: E/AndroidRuntime(7440): at dalvik.system.NativeStart.main(Native Method)
    04-23 13:03:22.957: E/AndroidRuntime(7440): Caused by: java.lang.NullPointerException
    04-23 13:03:22.957: E/AndroidRuntime(7440): at com.example.speechtotextdemo.MainActivity.onCreate(MainActivity.java:33)
    04-23 13:03:22.957: E/AndroidRuntime(7440): at android.app.Activity.performCreate(Activity.java:5104)
    04-23 13:03:22.957: E/AndroidRuntime(7440): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
    04-23 13:03:22.957: E/AndroidRuntime(7440): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
    04-23 13:03:22.957: E/AndroidRuntime(7440): ... 11 more

    ReplyDelete
  4. Please tes the given program with device. hope it will work. i will see the code again and will send you the correct code, if it is having problem.

    ReplyDelete
  5. now its working perfectly. i think there was some problm with my device. thanx

    ReplyDelete
  6. Oh That is really great than...have fun

    ReplyDelete