Showing posts with label Android SpeechToText. Show all posts
Showing posts with label Android SpeechToText. Show all posts

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