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

Sunday, 11 December 2016

TextToSpeech

 Android is providing Text To Speech and its speaks many different languages with different names, country or others.

Activiy_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">


<Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="speak" 
 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.speech.tts.TextToSpeech;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.Locale;

public class MainActivity extends AppCompatActivity {
Button speakbtn;
EditText input;
TextToSpeech tts;

@Override protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 speakbtn=(Button)findViewById(R.id.button);
 input=(EditText)findViewById(R.id.editText);
 tts=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.setOnClickListener(new 
                              View.OnClickListener() {
@Override public void onClick(View v) {
 String name=input.getText().toString();
 String s1="Android";
 tts.speak(s1,RESULT_OK,null);
 tts.setLanguage(Locale.UK);
 }
 });
 }
}

Output :-

Android Text To Speech
Android Text To Speech