Friday, December 26, 2014

Speech to text in android

Hi friends This is speech to text in android as below:-


Layout:-

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.speech.MainActivity" >

    <ListView
        android:id="@+id/lvOptions"
        android:layout_width="match_parent"
        android:layout_height="350dp"
        android:layout_alignParentLeft="true" >

    </ListView>

    <Button
        android:id="@+id/bSpeak"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="17dp"
        android:text="Speak" />

</RelativeLayout>







Java class:-


public class MainActivity extends ActionBarActivity {

Button speak;
    ListView options;
    ArrayList<String> results;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

 speak = (Button) findViewById(R.id.bSpeak);
       options = (ListView) findViewById(R.id.lvOptions);
 
       speak.setOnClickListener(new OnClickListener() {
 
           @Override
           public void onClick(View v) {
               // TODO Auto-generated method stub
               // This are the intents needed to start the Voice recognizer
               Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
               i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                       RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
               i.putExtra(RecognizerIntent.EXTRA_PROMPT, "Say something");
 
               startActivityForResult(i, 1010);
           }
       });
 
       // retrieves data from the previous state. This is incase the phones
       // orientation changes
       if (savedInstanceState != null) {
           results = savedInstanceState.getStringArrayList("results");
 
           if (results != null)
               options.setAdapter(new ArrayAdapter<String>(this,
                       android.R.layout.simple_list_item_1, results));
       }
   }
 
   @Override
   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
       // TODO Auto-generated method stub
       // retrieves data from the VoiceRecognizer
       if (requestCode == 1010 && resultCode == RESULT_OK) {
           results = data
                   .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
           options.setAdapter(new ArrayAdapter<String>(this,
                   android.R.layout.simple_list_item_1, results));
       }
 
       super.onActivityResult(requestCode, resultCode, data);
   }
 
   @Override
   protected void onSaveInstanceState(Bundle outState) {
       // This should save all the data so that when the phone changes
       // orientation the data is saved
       super.onSaveInstanceState(outState);
 
       outState.putStringArrayList("results", results);
   }
 
}


Thank you .....

No comments:

Post a Comment