0% found this document useful (0 votes)
7 views9 pages

Task 13.......

The document describes a text-to-speech feature with an XML layout containing edit texts, text views, seek bars and buttons. The Java code initializes the text-to-speech object and speaks the entered text on button click by setting the pitch and speed based on seek bar values.

Uploaded by

Aniket Shekokar
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)
7 views9 pages

Task 13.......

The document describes a text-to-speech feature with an XML layout containing edit texts, text views, seek bars and buttons. The Java code initializes the text-to-speech object and speaks the entered text on button click by setting the pitch and speed based on seek bar values.

Uploaded by

Aniket Shekokar
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/ 9

Task 13 – Text To Speech

Name: Aniket Sarang Shekokar

• Code:==

1. Chats Fragment 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/background_shape">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Text To Speech"
android:textColor="@color/white"
android:textSize="28sp"
android:textStyle="bold"
android:fontFamily="sans-serif-condensed"
android:gravity="center"
android:layout_marginTop="40sp"/>

<EditText
android:layout_width="match_parent"
android:layout_height="150dp"
android:id="@+id/et_chats_enter_your_text"
android:hint="Enter your text"
android:textColorHint="@color/white"
android:textColor="@color/white"
android:textSize="16sp"
android:textStyle="bold"
android:fontFamily="sans-serif-condensed"
android:layout_margin="16dp"
android:padding="16dp"
android:gravity="center"
android:inputType="textMultiLine"
android:background="@drawable/edittext_shape"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Choose the pitch"
android:textColor="@color/white"
android:textSize="16sp"
android:fontFamily="sans-serif-condensed"
android:layout_margin="16dp"/>

<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/sb_chats_pitch"
android:min="0"
android:max="100"
android:progress="50"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Choose the speed"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="@color/white"
android:fontFamily="sans-serif-condensed"
android:layout_margin="16dp"/>

<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/sb_chats_speed"
android:min="0"
android:max="100"
android:indeterminateTint="@color/white"
android:thumbTint="@color/white"
android:progress="50"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_chats_speak"
android:text="Speak"
android:textColor="@color/yellow"
android:backgroundTint="@color/white"
android:layout_gravity="center"
android:layout_marginTop="20dp"/>

</LinearLayout>

2. Chat Fragment.Java:--

package com.example.aniketsapp;

import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import java.util.Locale;

public class ChatsFragment extends Fragment {


EditText et_enter_your_text;
SeekBar sb_pitch,sb_speed;
Button btn_speak;
TextToSpeech textToSpeech;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_chats, container,
false);
et_enter_your_text =
view.findViewById(R.id.et_chats_enter_your_text);
sb_pitch = view.findViewById(R.id.sb_chats_pitch);
sb_speed = view.findViewById(R.id.sb_chats_speed);
btn_speak = view.findViewById(R.id.btn_chats_speak);

btn_speak.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
speakText();
}
});

textToSpeech = new TextToSpeech(getActivity(), new


TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS)
{
int result = textToSpeech.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA || result ==
TextToSpeech.LANG_NOT_SUPPORTED)
{
Toast.makeText(getActivity(),"The Langague is not
supported",Toast.LENGTH_SHORT).show();
}
else
{
speakText();
}
}
else
{
Toast.makeText(getActivity(),"Failed to
initialization",Toast.LENGTH_SHORT).show();
}

}
});
return view;

}
private void speakText() {
String text = et_enter_your_text.getText().toString();

if (text.isEmpty() || "".equals(text))
{
text = "Please Enter Some Text to Speak";
}
float pitch = (float) sb_pitch.getProgress()/50;
if (pitch <0.1)
{
pitch = 0.1f;
}
textToSpeech.setPitch(pitch);

float speed = (float) sb_speed.getProgress()/50;


if (speed <0.1)
{
speed = 0.1f;
}
textToSpeech.setSpeechRate(speed);

textToSpeech.speak(text,TextToSpeech.QUEUE_FLUSH,null,null);

}
}

Output:==

You might also like