Android Text to Speech Simple App | the latest tutorial is now and around Android

Android Text to Speech Simple App

Android Text to Speech Simple App - Hallo sahabat the latest tutorial is now and around Android, Pada Artikel yang anda baca kali ini dengan judul Android Text to Speech Simple App, kami telah mempersiapkan artikel ini dengan baik untuk anda baca dan ambil informasi didalamnya. mudah-mudahan isi postingan Artikel Android Tips and Tricks, Artikel Text to Speech, yang kami tulis ini dapat anda pahami. baiklah, selamat membaca.

Judul : Android Text to Speech Simple App
link : Android Text to Speech Simple App

Baca juga


Android Text to Speech Simple App




Hello friends, In this post I am going to show how to use Android Text To Speech feature in your project . This is a very interesting feature in which whenever you use this feature in your app, it will convert the text into speech i.e. speak out or pronounce the text you entered.

In this tutorial i also explained changing the language type, pitch level and speed level.  


1. Create a new project by going to File New Android Project. and fill required details.

2. Implement your MainActivity class from TextToSpeech.OnInitListener


3. Now add below code to your MainActivity.java
package gracious.text_to_speech;

import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.util.Locale;

public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener{

    private TextToSpeech mTextToSpeech;
    private Button mButton;
    private EditText mEditText;

    @Override
   
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        mTextToSpeech = new TextToSpeech(this, this);
        mButton = (Button) findViewById(R.id.button);
        mEditText = (EditText) findViewById(R.id.editText);

        // button on click event
       
mButton.setOnClickListener(new View.OnClickListener() {

            @Override
           
public void onClick(View arg0) {

                String text = mEditText.getText().toString();
                mTextToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null);
            }
        });
    }

    @Override
   
public void onInit(int status) {
        if (status == TextToSpeech.SUCCESS) {

            int result = mTextToSpeech.setLanguage(Locale.US);

            if (result == TextToSpeech.LANG_MISSING_DATA
                   
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
                //No action to be take place
           
} else {
                mButton.setEnabled(true);

                String text = mEditText.getText().toString();
                mTextToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null);
            }

        } else {
           //There is some error
       
}
    }

    @Override
   
public void onDestroy() {
        // Don't forget to shutdown tts!
       
if (mTextToSpeech != null) {
            mTextToSpeech.stop();
            mTextToSpeech.shutdown();
        }
        super.onDestroy();
    }
}


4. Now add below code to your content_main.xmlfile:-
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
   
xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:app="http://schemas.android.com/apk/res-auto"
   
xmlns:tools="http://schemas.android.com/tools"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
android:paddingBottom="@dimen/activity_vertical_margin"
   
android:paddingLeft="@dimen/activity_horizontal_margin"
   
android:paddingRight="@dimen/activity_horizontal_margin"
   
android:paddingTop="@dimen/activity_vertical_margin"
   
app:layout_behavior="@string/appbar_scrolling_view_behavior"
   
tools:context="gracious.text_to_speech.MainActivity"
   
tools:showIn="@layout/activity_main">

    <
EditText
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:id="@+id/editText" android:layout_alignParentTop="true"
       
android:layout_centerHorizontal="true" android:layout_marginTop="94dp"/>

    <
Button
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
        android:text="Speack out the Text"
       
android:id="@+id/button" android:layout_centerVertical="true"
       
android:layout_centerHorizontal="true"/>

</
RelativeLayout>


4. Add below code to your activity_main.xml
<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinatorLayout
   
xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:app="http://schemas.android.com/apk/res-auto"
   
xmlns:tools="http://schemas.android.com/tools"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
android:fitsSystemWindows="true"
   
tools:context="gracious.text_to_speech.MainActivity">

    <
android.support.design.widget.AppBarLayout
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:theme="@style/AppTheme.AppBarOverlay">

        <
android.support.v7.widget.Toolbar
           
android:id="@+id/toolbar"
            android:layout_width="match_parent"
           
android:layout_height="?attr/actionBarSize"
           
android:background="?attr/colorPrimary"
            
app:popupTheme="@style/AppTheme.PopupOverlay"/>

    </
android.support.design.widget.AppBarLayout>

    <
include layout="@layout/content_main"/>

    <
android.support.design.widget.FloatingActionButton
       
android:id="@+id/fab"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:layout_gravity="bottom|end"
       
android:layout_margin="@dimen/fab_margin"
       
android:src="@android:drawable/ic_dialog_email"/>

</
android.support.design.widget.CoordinatorLayout>


That's it, Test your code and app now.

Changing Language

You can change language to speak by using setLanguage() function. Lot of languages are supported like Canada, French, Chinese, Germany etc.
mTextToSpeech.setLanguage(Locale.CHINESE);  // To set as Chinese lang

Changing Pitch Rate

You can set speed pitch level by using setPitch() function. By default the value is 1.0 You can set lower values than 1.0 to decrease pitch level or greater values for increase pitch level.
mTextToSpeech.setPitch(0.5);

Changing Speed Rate

The speed rate can be set using setSpeechRate(). This also will take default of 1.0 value. You can double the speed rate by setting 2.0 or make half the speed level by setting 0.5
mTextToSpeech.setSpeechRate(2);





Demikianlah Artikel Android Text to Speech Simple App

Sekianlah artikel Android Text to Speech Simple App kali ini, mudah-mudahan bisa memberi manfaat untuk anda semua. baiklah, sampai jumpa di postingan artikel lainnya.

Anda sekarang membaca artikel Android Text to Speech Simple App dengan alamat link https://kokonghod.blogspot.com/2016/02/android-text-to-speech-simple-app.html

0 Response to "Android Text to Speech Simple App"