0% found this document useful (0 votes)
39 views

Program 7

This document provides code to create a simple text-to-speech Android application. It includes XML layout code with an EditText for user input and a "Convert Text to Speech" button. The Java code initializes a TextToSpeech object, sets the language to UK English, and defines an onClick method for the button that gets the EditText text and uses TextToSpeech to convert it to audio output.

Uploaded by

Moon Light
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Program 7

This document provides code to create a simple text-to-speech Android application. It includes XML layout code with an EditText for user input and a "Convert Text to Speech" button. The Java code initializes a TextToSpeech object, sets the language to UK English, and defines an onClick method for the button that gets the EditText text and uses TextToSpeech to convert it to audio output.

Uploaded by

Moon Light
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Program 7.

Develop a simple application with one EditText so that the user can
write some text in it. Create a button called “Convert Text to Speech” that
converts the user input text into voice.
XML Code:
<?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:textAlignment="center"
tools:context=".MainActivity">

<TextView
android:layout_width="291dp"
android:layout_height="58dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="67dp"
android:layout_marginBottom="617dp"
android:text="Text to Speech"
android:textAlignment="center"
android:textSize="36sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="113dp"
android:layout_marginBottom="484dp"
android:ems="10"
android:inputType="textPersonName"
android:hint="enter text here"
android:text="" />

<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:onClick="convert"
android:layout_marginEnd="185dp"
android:layout_marginBottom="359dp"
android:text="convert" />

</RelativeLayout>

 View.getContext(): Returns the context the view is currently running in.


Usually the currently active Activity.
 Activity.getApplicationContext(): Returns the context for the entire
application (the process all the Activities are running inside of). Use this
instead of the current Activity context if you need a context tied to the lifecycle
of the entire application, not just the current
Activity.getApplicationContext():
When we call a method or a constructor, we often have to pass a context and
often we use “this” to pass the activity context or “getApplicationContext” to
pass the application context. This method is generally used for the application
level and can be used to refer to all the activities

 ContextWrapper.getBaseContext(): If you need access to a Context from


within another context, you use a ContextWrapper. The Context referred to
from inside that ContextWrapper is accessed via getBaseContext().
 Android allows you convert your text into voice. Not only you can convert
it but it also allows you to speak text in variety of different languages.
 Android provides TextToSpeech class for this purpose. In order to use this
class, you need to instantiate an object of this class and also specify
the initListener. Its syntax is given below –
 private EditText write;
 ttobj=new TextToSpeech(getApplicationContext(), new
TextToSpeech.OnInitListener() {
 @Override
 public void onInit(int status) {
 }
 });
 In this listener, you have to specify the properties for TextToSpeech object , such
as its language ,pitch e.t.c.
 Language can be set by calling setLanguage() method. Its syntax is given
below −
 ttobj.setLanguage(Locale.UK);
 The method setLanguage takes an Locale object as parameter. The list of some
of the locales available are given below −

Sr.No Locale

1 US

2 CANADA_FRENCH

3 GERMANY

4 ITALY

5 JAPAN

6 CHINA

 Once you have set the language, you can call speak method of the class to
speak the text. Its syntax is given below −
 ttobj.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
 Apart from the speak method, there are some other methods available in the
TextToSpeech class. They are listed below −

Sr.No Method & description

1 addSpeech(String text, String filename)


This method adds a mapping between a string of text and a sound file.

2 getLanguage()
This method returns a Locale instance describing the language.

3 isSpeaking()
This method checks whether the TextToSpeech engine is busy speaking.
4 setPitch(float pitch)
This method sets the speech pitch for the TextToSpeech engine.

5 setSpeechRate(float speechRate)
This method sets the speech rate.

6 shutdown()
This method releases the resources used by the TextToSpeech engine.

7 stop()
This method stop the speak.

JAVA Code
package com.example.lb7;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.util.Locale;

public class MainActivity extends AppCompatActivity {


EditText e1;
TextToSpeech t1;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1=findViewById(R.id.editText);
t1=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status)
{
if(status!=TextToSpeech.ERROR)
{
t1.setLanguage(Locale.UK);
}
}
});
}
public void convert(View V)
{
String tospeak=e1.getText().toString();
t1.speak(tospeak,TextToSpeech.QUEUE_FLUSH,null);
}
}

https://www.youtube.com/watch?v=0ENHaFUkdf8&t=609s program7 YouTube link

You might also like