Speech To Text in Android


AndroidManifest.xml

< ?xml version="1.0" encoding="utf-8"?>
< manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.firstapp">

    < application
        android:name=".SingleTask"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        < activity android:name=".MainActivity">
            < intent-filter>
                < action android:name="android.intent.action.MAIN" />

                < category android:name="android.intent.category.LAUNCHER" />
            < /intent-filter>
        < /activity>
    < /application>
< /manifest>

activity_main.xml


< RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    < TextView
        android:id="@+id/mytext"
        android:padding="20dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:typeface="serif"
        android:layout_above="@id/mybtn"
        android:hint="Say Something..."
        android:inputType="textMultiLine" />

    < Button
        android:id="@+id/mybtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="click"
        android:padding="20dp"
        android:layout_alignParentBottom="true"
        android:text="Press Me" />


< /RelativeLayout>

MainActivity.java

package com.example.firstapp;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ActivityNotFoundException;
import android.content.BroadcastReceiver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.speech.tts.TextToSpeech;
import android.speech.tts.Voice;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Locale;

public class MainActivity extends AppCompatActivity {

    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.mytext);
    }

    public void click(View v) {
        getSpeak();

    }

    private void getSpeak() {
        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
        RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
        intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
                "Say Something..");
        try {
            startActivityForResult(intent, 1001);
        } catch (ActivityNotFoundException a) {
            Toast.makeText(getApplicationContext(),
                    a.toString(),
                    Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode,Intent data)
    {
     super.onActivityResult(requestCode, resultCode, data);
     switch (requestCode) {
         case 1001: {
            if (resultCode == RESULT_OK && null != data) {

               ArrayList result = data
                .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                  textView.setText(result.get(0));
                }
                break;
            }

        }
    }
}

No comments: