Welcome Friends !!
I am Ajay Jaiswal . I am going to talk something that always amuses us ... letting the mobile devices to speak and talk to us . While sending any sms or email we want something that is very flexible and we don't have to type for a long message and then send them.
We want something that can interpret what we are saying and convert them into the regular text that we could have typed. We want a application that can convert our speech into text and in return convert a text into speech and talk with us.
Here i am going to write such article that enable us to do so. Android provide us a lot big way to what we want and build awesome cool applications.
Android provide us a whole bunch of library and classes for this text to speech and sppech to text conversion.
It provide us the class :
public class TextToSpeech extends object
The whole more detail about the class and its various method and constants can be found on the Android Developer Dashboard.
A TextToSpeech instance can only be used to synthesize text once it has completed its initialization. Implement the
When you are done using the TextToSpeech instance, call the
TextToSpeech.OnInitListener
to be notified of the completion of the initialization.When you are done using the TextToSpeech instance, call the
shutdown()
method to release the native resources used by the TextToSpeech engine.
I am going to build up a simple application illustrating the use of this feature. The idea could be used for developing many cool and awesome applications.
The source code need or the demo .apk if asked for will be provided through email.
For any queries or source code you can email me at : ajayjaiswalhi@gmail.com
For running this application we need to have TextToSpeech support for our android device. The application could be tested on the usual android emulator.
If TextToSpeech support is not provided in your device you will have a log message that Locale / Language not supported.
You need to update your device by installing TTS service form Google Play Store.
1. Create a new project by going to File ⇒ New Android Project. and fill required details.
2. Implement your main Activity class from TextToSpeech.OnInitListener
public
class
AndroidTextToSpeechActivity
extends
Activity
implements
TextToSpeech.OnInitListener {
==>> You need to import following classes
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;
The whole source code for the application is :
public
class
AndroidTextToSpeechActivity
extends
Activity
implements
TextToSpeech.OnInitListener {
/** Called when the activity is first created. */
private
TextToSpeech tts;
private
Button btnSpeak;
private
EditText txtText;
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.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
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"
,
"This Language is not supported"
);
}
else
{
btnSpeak.setEnabled(
true
);
speakOut();
}
}
else
{
Log.e(
"TTS"
,
"Initilization Failed!"
);
}
}
private
void
speakOut() {
String text = txtText.getText().toString();
tts.speak(text, TextToSpeech.QUEUE_FLUSH,
null
);
}
}
4. Now run your project and test your app by entering some text in input filed.
Changing Language
You can change language to speak by using setLanguage() function. Lot of languages are supported like Canada, French, Chinese, Germany etc.
tts.setLanguage(Locale.CHINESE);
// Chinese language
No comments:
Post a Comment