Text To Speech 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: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

< ?xml version="1.0" encoding="utf-8"?>
< 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:gravity="center"
        android:textStyle="bold"
        android:textSize="20sp"
        android:text="Welcome to Android world"
        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 {
    TextToSpeech tts;
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.mytext);
        tts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status != TextToSpeech.ERROR) {
                    tts.setLanguage(Locale.US);
                }
            }
        });
    }

    public void click(View v) {
        String msg = textView.getText().toString().trim();
        tts.speak(msg, TextToSpeech.QUEUE_FLUSH, null, null);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        tts.stop();
    }
}

No comments: