We've inroduced a new feature in Android platform:Text-To-Speech(TTS),Also known as "speech synthesis",TTs enables your Android device to"speak" text of different languges.
Text-to-speech can help you push your app in new directions.Whether you use TTs to help users with disabilities,to enable the use of your application while looking from the screen,or simply to make it cool, we hope you'll enjoy this new feature.
package speek.Codingonandroid;
import android.app.Activity;
import android.app.Instrumentation.ActivityResult;
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;
public class TExttoSpeechActivity extends Activity implements OnClickListener, OnInitListener {
Button spe;
EditText ed;
TextToSpeech ts;
int MY_DATA_CHECK_CODE;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
spe=(Button)findViewById(R.id.button1);
ed=(EditText)findViewById(R.id.editText1);
Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
// startActivity(i);
spe.setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
String st = ed.getText().toString();
if(st!=null && st.length()>0)
{
ts.speak(st, TextToSpeech.QUEUE_ADD, null);
}
}
protected void onActivityResult(
int requestCode, int resultCode, Intent data) {
if (requestCode == MY_DATA_CHECK_CODE) {
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
// success, create the TTS instance
ts = new TextToSpeech(this, this);
} else {
// missing data, install it
Intent installIntent = new Intent();
installIntent.setAction(
TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installIntent);
}
}
}
public void onInit(int status) {
// TODO Auto-generated method stub
}
}
Your .xml is the following:
<?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" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="speek" />
</LinearLayout>
Use this in your Applications for all apps:
Text-to-speech can help you push your app in new directions.Whether you use TTs to help users with disabilities,to enable the use of your application while looking from the screen,or simply to make it cool, we hope you'll enjoy this new feature.
package speek.Codingonandroid;
import android.app.Activity;
import android.app.Instrumentation.ActivityResult;
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;
public class TExttoSpeechActivity extends Activity implements OnClickListener, OnInitListener {
Button spe;
EditText ed;
TextToSpeech ts;
int MY_DATA_CHECK_CODE;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
spe=(Button)findViewById(R.id.button1);
ed=(EditText)findViewById(R.id.editText1);
Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
// startActivity(i);
spe.setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
String st = ed.getText().toString();
if(st!=null && st.length()>0)
{
ts.speak(st, TextToSpeech.QUEUE_ADD, null);
}
}
protected void onActivityResult(
int requestCode, int resultCode, Intent data) {
if (requestCode == MY_DATA_CHECK_CODE) {
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
// success, create the TTS instance
ts = new TextToSpeech(this, this);
} else {
// missing data, install it
Intent installIntent = new Intent();
installIntent.setAction(
TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installIntent);
}
}
}
public void onInit(int status) {
// TODO Auto-generated method stub
}
}
Your .xml is the following:
<?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" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="speek" />
</LinearLayout>
Use this in your Applications for all apps:
No comments:
Post a Comment