Sunday, 11 December 2016

SpeechToText

 Android Speech To Text its similar to Android Text To Speech, but here in this code reverse to text to speech and shown example code below.

Activity_main.xml :-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
 xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:orientation="vertical" 
 android:textAlignment="center">
<TextView 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:textAppearance="?android:attr/textAppearanceLarge" 
 android:text="Android SpeechToText " 
 android:id="@+id/textView" 
 android:layout_gravity="center_horizontal" />

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

<EditText 
 android:layout_width="124dp" 
 android:layout_height="wrap_content" 
 android:id="@+id/editText" 
 android:layout_gravity="center_horizontal" 
 android:hint="input" />

</LinearLayout>

MainActivity.java :-

package com.kiranapp;

import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.support.v7.app.AppCompatActivity;
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 speak;
    TextView input;
@Override 
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 speak=(Button)findViewById(R.id.button);
 input=(TextView)findViewById(R.id.textView);
 speak.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, "Dude Your phone does not 
                            support Speech to Text",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);
 input.setText(al.get(0));
 }
 }
 super.onActivityResult(requestCode, resultCode, data);
 }
 }

Output :-

Android Speech To Text
Android Speech To Text

2 comments:

  1. Thanks a lot! You made a new blog entry to answer my question; I really appreciate your time and effort.
    Android training in chennai with placement | Android Training in chennai |Android Training in Velachery

    ReplyDelete
  2. 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..Android Training in chennai | Best Android Training in chennai|Android Training in chennai with placement | Android Training in velachery

    ReplyDelete