1) Create a new Android project go to
file-> new Android application-> Type the Application name as:- TextToSpeech
package name as:-com.example.texttospeech
then click next

2) Then follow these steps:-

click next
3)
4)
5) click next and change the Activity name according to your need in this project.
I have used the default name as activity_main.

and click finish
6) The layout file will open
click to activity_main.xml next to graphical layout tab
and write this code.
</LinearLayout>
7) Go to values folder -> Strings.xml in that put these fields
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">TextToSpeech</string>
<string name="btnSpeak">Speak Out</string>
<string name="action_settings">Settings</string>
<string name="editText">enter your text</string>
</resources>
8) Go to source folder open MainActivity.java
and write this code.
file-> new Android application-> Type the Application name as:- TextToSpeech
package name as:-com.example.texttospeech
then click next

2) Then follow these steps:-

click next
3)
4)

5) click next and change the Activity name according to your need in this project.
I have used the default name as activity_main.

and click finish
6) The layout file will open
click to activity_main.xml next to graphical layout tab
and write this code.
<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" >
<EditText
android:id="@+id/txtText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/editText" />
<Button
android:id="@+id/btnSpeak"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp"
android:text="@string/btnSpeak" />
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/txtText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/editText" />
<Button
android:id="@+id/btnSpeak"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp"
android:text="@string/btnSpeak" />
</LinearLayout>
7) Go to values folder -> Strings.xml in that put these fields
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">TextToSpeech</string>
<string name="btnSpeak">Speak Out</string>
<string name="action_settings">Settings</string>
<string name="editText">enter your text</string>
</resources>
8) Go to source folder open MainActivity.java
and write this code.
package
com.example.texttospeech;
import
java.util.Locale;
import
android.speech.tts.TextToSpeech;
import
android.app.Activity;
import
android.os.Bundle;
import
android.util.Log;
import
android.view.View;
import
android.widget.Button;
import
android.widget.EditText;
public
class MainActivity extends Activity implements
TextToSpeech.OnInitListener {
private
TextToSpeech tts;
private
Button btnSpeak;
private
EditText txtText;
@Override
protected
void onCreate(Bundle savedInstanceState) {
//
TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tts
= new TextToSpeech(this, this);
btnSpeak
= (Button) findViewById(R.id.btnSpeak);
txtText
= (EditText) findViewById(R.id.txtText);
//
button on click event
btnSpeak.setOnClickListener(new
View.OnClickListener() {
@Override
public
void onClick(View arg0) {
speakOut();
}
});
}
@Override
protected
void onDestroy() {
//
TODO Auto-generated method stub
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",
"This Language is not supported");
}
else {
btnSpeak.setEnabled(true);
speakOut();
}
}
else {
Log.e("TTS",
"Initilization Failed!");
}
}
private
void speakOut() {
//
TODO Auto-generated method stub
String
text = txtText.getText().toString();
tts.speak(text,
TextToSpeech.QUEUE_FLUSH, null);
}
}
9) Your
done with this now compile your project and run it.
Before
click on run connect your android phone to your PC and in your
mobile
Developer settings
click on USB Debugging option.
Note: In
some of the phones Text to speech engines are not installed you
have to installed it
through Google play
store.
else
you can use the following code to check it .
Intent
intent =
new
Intent
();
intent.setAction(
TextToSpeech
.
Engine
.ACTION_CHECK_TTS_DATA);
startActivityForResult(intent,
0
);
@Override
protected
void
onActivityResult(
int
requestCode,
int
resultCode,
Intent
data) {
if
(requestCode ==
0
){
if
(resultCode ==
TextToSpeech
.
Engine
.CHECK_VOICE_DATA_PASS){
Toast
.makeText(getApplicationContext(),
"Already Installed"
,
Toast
.LENGTH_LONG).show();
}
else
{
Intent
installIntent =
new
Intent
();
installIntent.setAction(
TextToSpeech
.
Engine
.ACTION_INSTALL_TTS_DATA);
startActivity(installIntent);
Toast
.makeText(getApplicationContext(),
"Installed Now"
,
Toast
.LENGTH_LONG).show();
}
No comments:
Post a Comment