Sunday, 1 November 2015

Speech to Text Example

Android comes with an inbuilt feature speech to text through which you can provide speech input to your app. With this you can add some of the cool features to your app like adding voice navigation(Helpful when you are targeting disabled people), filling a form with voice input etc.,
activity_main.xml
-------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:tools="http://schemas.android.com/tools"
   
xmlns:app="http://schemas.android.com/apk/res-auto"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
app:layout_behavior="@string/appbar_scrolling_view_behavior"
   
tools:showIn="@layout/activity_main"
   
tools:context=".MainActivity"
   
android:orientation="vertical">

    <
TextView
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:textAppearance="?android:attr/textAppearanceLarge"
       
android:text="Speech to Text Example"
       
android:id="@+id/textView"
       
android:layout_gravity="center_horizontal" />

    <
Button
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text="Speak"
       
android:id="@+id/button"
       
android:layout_gravity="center_horizontal"
       
android:layout_marginTop="10dp" />

    <
TextView
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:textAppearance="?android:attr/textAppearanceLarge"
       
android:text="Display"
       
android:id="@+id/textView2"
       
android:layout_marginTop="10dp" />
</
LinearLayout>
 MainActivity.Java
------------------------------
package com.android.anil.speechtotext;

import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    Button
speakbtn;
    TextView
display;
   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.
toolbar);
        setSupportActionBar(toolbar);
       
speakbtn=(Button)findViewById(R.id.button);
       
display=(TextView)findViewById(R.id.textView2);
       
speakbtn.setOnClickListener(new View.OnClickListener() {
           
@Override
           
public void onClick(View v) {
                Intent intent=
new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                intent.putExtra(RecognizerIntent.
EXTRA_LANGUAGE_MODEL,"en_US");
               
try {
                    startActivityForResult(intent,
1);
                }
catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(MainActivity.
this, "Sorry Your Device does not support Speech to Text feautre", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

   
@Override
   
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
       
if(requestCode==1){
        
if(resultCode==RESULT_OK&&data!=null) {
             ArrayList<String> al=data.getStringArrayListExtra(RecognizerIntent.
EXTRA_RESULTS);
            
display.setText(al.get(0));
         }
        }
       
super.onActivityResult(requestCode, resultCode, data);
    }
}
Output is:
-------------------
Note: You need to run it on the Real Device (example: Mobile) to Test the application .
           Android emulator does not support the speech to text feautre






No comments:

Post a Comment