Hello Guys, Hope You all are doing well. Today I am going to show an example on Voice recognition in Android. My Previous Post was on Text To speech . Now I am going to show Speech to text and search it on Google.
Android natively provides feature of Speech to Text.Voice recognition featured in android is achieved using the RecognizerIntent.Use the Recognizer class in an intent to call the voice API.Internally voice recognition communicates with the server and gets the results. So you must provide the internet access permission for the application. Android Jelly Bean(API level 16) doesn’t require internet connection to perform voice recognition.
Here we check for the recognizer in the device if available the speech to text happen else we show a toast displaying 'Recognizer Not Found'
Note: This application will not work in emulator since it does not have the recognizer, so test it in a device and it also requires an internet connection.
Above given image is final output of voice recognition working sample app. Just create a Android project named it VoiceRecognition in your Eclipse. Copy and paste following source code in your class.
Java Class
1. VoiceActivity.java
Xml file
1. activity_voice.xml
Now put following code in your
Voice Activity. java.
packagecom.sks.texttospeech;
importjava.util.ArrayList;
importjava.util.List;
importandroid.app.Activity;
importandroid.app.SearchManager;
importandroid.content.Intent;
importandroid.content.pm.PackageManager;
importandroid.content.pm.ResolveInfo;
importandroid.os.Bundle;
importandroid.speech.RecognizerIntent;
importandroid.view.View;
importandroid.widget.AdapterView;
importandroid.widget.AdapterView.OnItemClickListener;
importandroid.widget.ArrayAdapter;
importandroid.widget.Button;
importandroid.widget.ListView;
importandroid.widget.Toast;
publicclassVoiceActivity extendsActivity{
privatestaticfinalintREQUEST_CODE= 1234;
privateListView matchList;
Button speak_button;
@Override
protectedvoidonCreate(Bundle savedInstanceState) {
// TODOAuto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_voice_recignition);
speak_button= (Button) findViewById(R.id.speakButton);
matchList= (ListView) findViewById(R.id.list);
// Disable button if no recognition service is present
PackageManager pm = getPackageManager();
List<ResolveInfo> activities = pm.queryIntentActivities(
newIntent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
if(activities.size() == 0)
{
speak_button.setEnabled(false);
Toast.makeText(getApplicationContext(), "Recognizer Not Found", 1000).show();
}
speak_button.setOnClickListener(newView.OnClickListener() {
@Override
publicvoidonClick(View v) {
// TODOAuto-generated method stub
startVoiceRecognitionActivity();
}
});
matchList.setOnItemClickListener(newOnItemClickListener() {
@Override
publicvoidonItemClick(AdapterView<?> parent, View view, intposition,longid) {
String item = matchList.getAdapter().getItem(position).toString();
Toast.makeText(VoiceActivity.this, item + " selected", Toast.LENGTH_LONG).show();
Intent search = newIntent(Intent.ACTION_WEB_SEARCH);
search.putExtra(SearchManager.QUERY, item);
startActivity(search);
}
});
}
privatevoidstartVoiceRecognitionActivity()
{
Intent intent = newIntent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
//1.LANGUAGE_MODEL_WEB_SEARCH : For short phrases
//2.LANGUAGE_MODEL_FREE_FORM : If not sure about the words or phrases and its domain.
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "saurabhsharma Voice Recognition...");
startActivityForResult(intent, REQUEST_CODE);
}
@Override
protectedvoidonActivityResult(intrequestCode, intresultCode, Intent data)
{
if(requestCode == REQUEST_CODE&& resultCode == RESULT_OK)
{
ArrayList<String> matches = data.getStringArrayListExtra(
RecognizerIntent.EXTRA_RESULTS);
matchList.setAdapter(newArrayAdapter<String>(this, android.R.layout.simple_list_item_1,matches));
}
super.onActivityResult(requestCode, resultCode, data);
}
}
Now Put following code in your
activity_voice.xml
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="center_horizontal"
android:textStyle="bold"
android:textColor="#A52A2A"
android:text="Click microPhone and start speaking" />
<Button
android:id="@+id/speakButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@drawable/voic_img1" />
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"/>
</LinearLayout>
Finally put internet permission in your manifest file.
<uses-permissionandroid:name="android.permission.INTERNET"/>
Run this code on Actual Device and Enjoy the Speech Api Of android.
Happy Coding !!!
0 komentar:
Posting Komentar