Voice Recognition
- Get link
- X
- Other Apps
This post about how to search voice based search places instead typing the text.
Source Code
Call Voice recognition
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Tell me what you want");
Retrun Result Method
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == REQUEST_RECOGNIZE && resultCode == Activity.RESULT_OK) {
ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
StringBuilder sb = new StringBuilder();
for(String piece : matches) {
sb.append(piece);
sb.append('\n');
}
if(matches.size() !=0){
for(int i = 0; i<matches.size(); i++){
text.setText("**********Single Values************ /n"+matches.get(0));
}
}
} else {
Toast.makeText(this, "Operation Canceled", Toast.LENGTH_SHORT).show();
}
}
Full Source Code
import java.util.ArrayList;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private static final int REQUEST_RECOGNIZE = 100;
TextView text;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
text = new TextView(this);
setContentView(text);
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Tell me what you want");
try {
startActivityForResult(intent, REQUEST_RECOGNIZE);
} catch (ActivityNotFoundException e) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Not Available");
builder.setMessage("No recognition software installed.Download ?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent marketIntent = new Intent(Intent.ACTION_VIEW);
marketIntent.setData(Uri.parse("https://play.google.com/store/apps/details?id=com.google.android.voicesearch"));
}
});
builder.setNegativeButton("No", null);
builder.create().show();
}
}
/**
* Handle the results from the voice recognition activity.
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == REQUEST_RECOGNIZE && resultCode == Activity.RESULT_OK) {
ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
StringBuilder sb = new StringBuilder();
for(String piece : matches) {
sb.append(piece);
sb.append('\n');
}
if(matches.size() !=0){
for(int i = 0; i<matches.size(); i++){
text.setText("**********Single Values************ /n"+matches.get(0));
}
}
} else {
Toast.makeText(this, "Operation Canceled", Toast.LENGTH_SHORT).show();
}
}
}
Comments
Post a Comment
Please post comments here:-)