Blog Details

img
Design

What is text to speech android

Spoke Right / 19 Nov, 2023

The TextToSpeech class in Android is used to convert a text into speech. We can also playback the speech and can create a sound file, once the text is converted to speech.

The constructor of the TextToSpeech class:

TextToSpeech(Context context, TextToSpeech.OnInitListener)

Methods of TextToSpeech class:

The Android TextToSpeech class contains various methods. The popular methods of the TextToSpeech class are described below. These are:

MethodUses
int speak (String text, int queueMode, HashMap params)Used to convert the text into speech. Queue Mode: QUEUE_ADD or QUEUE_FLUSH. Request parameters: null, KEY_PARAM_STREAM, KEY_PARAM_VALUME etc.
int setSpeechRate(float speed)Used to set the speed for the speech.
int setPitch(float speed)Used to set the pitch for the speech.
int setLanguage (Locale loc)Used to set the locale specific language for the speech.
void shutdown()Used to release the resource set by TextToSpeech Engine.
int stop()Used to interrupt the current utterance, whether it is played or rendered to file and to discard the other utterances in the queue.

TextToSpeech.OnInitListener Interface:

To perform event handling on the TextToSpeech engine, the TextToSpeech.OnInitListener interface needs to be implemented.

Method of TextToSpeech.OnInitListener Interface:

TextToSpeech.OnInitListener interface contains only one method. It is described below.

MethodUses
void onInit (int status)Used to signal the completion of the TextToSpeech engine initialization. Value of the status parameter: SUCCESS or ERROR.

Android TextToSpeech Example 1:

In the below example, we are demonstrating the usage of the Android TextToSpeech class to convert a text into speech.

activity_main.xml:

In the activity_main.xml file, we will drag a TextView, an EditText, and a button from the palette.


 
 
 
 
 
 
 
 
 
 

Activity class:(File: MainActivity.java)

In the MainActivity. java file, we will write the code to speak the written text.

package com.example.radioapp;
 
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import java.util.Locale;
 
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements
        TextToSpeech.OnInitListener {
    /** Called when the activity is first created. */
 
    private TextToSpeech tts;
    private Button buttonSpeak;
    private EditText editText;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        tts = new TextToSpeech(this, this);
        buttonSpeak = (Button) findViewById(R.id.button1);
        editText = (EditText) findViewById(R.id.editText1);
 
        buttonSpeak.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                speakOut();
            }
 
        });
    }
 
    @Override
    public void onDestroy() {
// Don't forget to shutdown tts!
        if (tts != null) {
            tts.stop();
            tts.shutdown();
        }
        super.onDestroy();
    }
 
    @Override
    public void onInit(int status) {
 
        if (status == TextToSpeech.SUCCESS) {
 
            int result = tts.setLanguage(Locale.US);
 
            if (result == TextToSpeech.LANG_MISSING_DATA
                    || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Log.e("TTS", "Language not supported");
            } else {
                buttonSpeak.setEnabled(true);
                speakOut();
            }
 
        } else {
            Log.e("TTS", "Failed!");
        }
 
    }
 
    private void speakOut() {
        String text = editText.getText().toString();
        tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }
 
}

0 comments

Warning: PHP Startup: Unable to load dynamic library 'imagick.so' (tried: /usr/local/lib/php/extensions/no-debug-non-zts-20210902/imagick.so (/usr/local/lib/php/extensions/no-debug-non-zts-20210902/imagick.so: cannot open shared object file: No such file or directory), /usr/local/lib/php/extensions/no-debug-non-zts-20210902/imagick.so.so (/usr/local/lib/php/extensions/no-debug-non-zts-20210902/imagick.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0