Home / Android / Text to speech project in Android

Text to speech project in Android

Text to speech project in android is the most commonly built project where the user provides the text to the application and

tts.speak(ggg, TextToSpeech.QUEUE_FLUSH, null);

Download Text to Speech project package.zip

Texttospeechactivity.Java

package j.l;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class TexttospeechActivity extends Activity implements OnClickListener,OnInitListener{
    /** Called when the activity is first created. */
    TextToSpeech tts;
    EditText e1;
    Button b1;
    int request=3;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        e1=(EditText)findViewById(R.id.et1);
        b1=(Button)findViewById(R.id.bt1);
        b1.setOnClickListener(this);
        Intent i=new Intent();
        i.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
        startActivityForResult(i, request);

    }
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
	if(requestCode==request)
	{
		if(resultCode==TextToSpeech.Engine.CHECK_VOICE_DATA_PASS)
		{
			tts=new TextToSpeech(this, this);
		}
		else
		{
			Intent i=new Intent();
			i.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
			startActivity(i);
		}
	}
	}
	@Override
	public void onInit(int status) {
		// TODO Auto-generated method stub
		if(status==TextToSpeech.SUCCESS)
		{
			Toast.makeText(getApplicationContext(),
					"engin install", 1000).show();
		}
		else if(status==TextToSpeech.ERROR)
		{
			Toast.makeText(getApplicationContext(), 
					"not install", 1000).show();

		}
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		String ggg=e1.getText().toString();
		if(ggg.length()>0 && ggg!=null)
		{
			tts.speak(ggg, TextToSpeech.QUEUE_FLUSH, null);
		}
	}
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
<EditText android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/et1"/>
<Button android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/bt1"
    android:text="ok"/>

</LinearLayout>

OUTPUT:

convert text to speech in android
Text to speech project in android

Leave a Reply

Your email address will not be published. Required fields are marked *

Preventing spam * Time limit is exhausted. Please reload the CAPTCHA.