May 11, 2011

Start Android system's voice recognizer using Intent

Result of Android system's voice recognizer

package com.AndroidRecognizer;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

public class AndroidRecognizer extends Activity {

Button startRecognizer;
Spinner spinnerResult;

private static final int RQS_RECOGNITION = 1;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startRecognizer = (Button)findViewById(R.id.startrecognizer);
spinnerResult = (Spinner)findViewById(R.id.result);

startRecognizer.setOnClickListener(startRecognizerOnClickListener);

}

private Button.OnClickListener startRecognizerOnClickListener
= new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
"Speech to Recognize");
startActivityForResult(intent, RQS_RECOGNITION);
}
};

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if((requestCode == RQS_RECOGNITION) & (resultCode == RESULT_OK)){

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

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, result);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerResult.setAdapter(adapter);

}

}
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/startrecognizer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Start Recognizer"
/>
<Spinner
android:id="@+id/result"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>


1 comment:

  1. I want recognizer to trigger automatically when i speak and stop when it senses silence and then show what i spoke

    ReplyDelete

Infolinks In Text Ads