Hello Guys , how are you today. In this Blog, we are going to see how to work with android TextToSpeech. So lets get started.
Download TextToSpeech
1) First make an Android Application Project.
2) Open "res/layout/activity_main.xml" and create an EditText and a Button there. For our case the code is below.
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical" >
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/word"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Say it"
android:id="@+id/say"/>
</LinearLayout>
3) Now open "src/your_package_name/MainActivity.java" and create a TextToSpeech Object. I will tell you how to do that .
TextToSpeech tts;
4) Now implement MainActivity to TextToSpeech.OnInitListener. In eclipse you will see error which can be removed by clicking on "add unimplemented function" . So the new function looks like.
@Override
public void onInit(int status) {
// TODO Auto-generated method stub
if(status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.US);
tts.setPitch((float) 60.0);
if(result==TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
Toast.makeText(this, "Languauge Not Supported", Toast.LENGTH_LONG).show();
Log.e("TTS", "Languauge Not Supported");
}else {
}
}else {
Toast.makeText(this, "Device Not Supported for Text To Speech", Toast.LENGTH_LONG).show();
Log.e("TTS", "Initialisation failed");
}
}
I think this function is self explanatory.
5) The device can be made to speak using the following single line code.
tts.speak(word.getText().toString(), TextToSpeech.QUEUE_FLUSH, null);
6) The whole code looks like below.
import java.util.Locale;
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements TextToSpeech.OnInitListener,OnClickListener {
EditText word;
Button say;
TextToSpeech tts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
word = (EditText) findViewById(R.id.word);
say = (Button) findViewById(R.id.say);
tts = new TextToSpeech(this, this);
say.setOnClickListener(this);
}
@Override
public void onInit(int status) {
// TODO Auto-generated method stub
if(status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.US);
tts.setPitch((float) 60.0);
if(result==TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
Toast.makeText(this, "Languauge Not Supported", Toast.LENGTH_LONG).show();
Log.e("TTS", "Languauge Not Supported");
}else {
}
}else {
Toast.makeText(this, "Device Not Supported for Text To Speech", Toast.LENGTH_LONG).show();
Log.e("TTS", "Initialisation failed");
}
}
private void speak() {
tts.speak(word.getText().toString(), TextToSpeech.QUEUE_FLUSH, null);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()) {
case R.id.say :speak();break;
}
}
}
Now we are done. For any queries pls comment .
Thank You
For More Info visit https://www.facebook.com/androidcoolstuffs
Download TextToSpeech
1) First make an Android Application Project.
2) Open "res/layout/activity_main.xml" and create an EditText and a Button there. For our case the code is below.
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical" >
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/word"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Say it"
android:id="@+id/say"/>
</LinearLayout>
3) Now open "src/your_package_name/MainActivity.java" and create a TextToSpeech Object. I will tell you how to do that .
TextToSpeech tts;
4) Now implement MainActivity to TextToSpeech.OnInitListener. In eclipse you will see error which can be removed by clicking on "add unimplemented function" . So the new function looks like.
@Override
public void onInit(int status) {
// TODO Auto-generated method stub
if(status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.US);
tts.setPitch((float) 60.0);
if(result==TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
Toast.makeText(this, "Languauge Not Supported", Toast.LENGTH_LONG).show();
Log.e("TTS", "Languauge Not Supported");
}else {
}
}else {
Toast.makeText(this, "Device Not Supported for Text To Speech", Toast.LENGTH_LONG).show();
Log.e("TTS", "Initialisation failed");
}
}
I think this function is self explanatory.
5) The device can be made to speak using the following single line code.
tts.speak(word.getText().toString(), TextToSpeech.QUEUE_FLUSH, null);
6) The whole code looks like below.
import java.util.Locale;
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements TextToSpeech.OnInitListener,OnClickListener {
EditText word;
Button say;
TextToSpeech tts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
word = (EditText) findViewById(R.id.word);
say = (Button) findViewById(R.id.say);
tts = new TextToSpeech(this, this);
say.setOnClickListener(this);
}
@Override
public void onInit(int status) {
// TODO Auto-generated method stub
if(status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.US);
tts.setPitch((float) 60.0);
if(result==TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
Toast.makeText(this, "Languauge Not Supported", Toast.LENGTH_LONG).show();
Log.e("TTS", "Languauge Not Supported");
}else {
}
}else {
Toast.makeText(this, "Device Not Supported for Text To Speech", Toast.LENGTH_LONG).show();
Log.e("TTS", "Initialisation failed");
}
}
private void speak() {
tts.speak(word.getText().toString(), TextToSpeech.QUEUE_FLUSH, null);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()) {
case R.id.say :speak();break;
}
}
}
Now we are done. For any queries pls comment .
Thank You
For More Info visit https://www.facebook.com/androidcoolstuffs
0 comments:
Post a Comment