Thursday 29 October 2015

Text to Speech in Android

Android is providing a cool feature (from Android 1.6) called Text to Speech (TTS) which speaks the text in different languages. This tutorial explains how to work with android text to speech or android speech synthesis. In this tutorial i also explained changing the language type, pitch level and speed level.
activity_main.xml
---------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
   
xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:tools="http://schemas.android.com/tools"
   
xmlns:app="http://schemas.android.com/apk/res-auto"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
app:layout_behavior="@string/appbar_scrolling_view_behavior"
   
tools:showIn="@layout/activity_main"
   
tools:context=".MainActivity"
   
android:orientation="vertical">

    <
EditText
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:id="@+id/editText"
       
android:hint="enter mame"
       
android:layout_marginTop="5dp" />

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

MainActivity.Java
-----------------------------
package com.android.anil.texttospeech;

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 android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    Button
speakbtn;
    EditText
name;
    TextToSpeech
textToSpeech;

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.
toolbar);
        setSupportActionBar(toolbar);
       
textToSpeech=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
           
@Override
           
public void onInit(int status) {
                Toast.makeText(MainActivity.
this, "Text to Speech engine is intialized", Toast.LENGTH_SHORT).show();
            }
        });
        
speakbtn=(Button)findViewById(R.id.button);
       
name=(EditText)findViewById(R.id.editText);
       
speakbtn.setOnClickListener(new View.OnClickListener() {
           
@Override
           
public void onClick(View v) {
                String s1=
name.getText().toString();
               
textToSpeech.speak(s1,RESULT_OK,null);
            }
        });
    }
}

Output is:
---------------------------

No comments:

Post a Comment

Encryption Decryption Example Android Using AES Alogirthm

activity_main.xml ------------------------- <?xml version="1.0" encoding="utf-8"?> <LinearLayou...