A TextToSpeech instance can only be used to synthesize text once it has completed its initialization. Implement 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.
Example:
Code:
activity_main.xml
<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:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="20dp"
        android:ems="10" >
        <requestFocus />
    </EditText>
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="58dp"
        android:layout_height="72dp"
        android:layout_gravity="right"
        android:layout_marginRight="50dp"
        android:layout_marginTop="30dp"
        android:src="@drawable/audio_speak" />
</LinearLayout>
MainActivity.java
package in.androiddevelopmentanddiscussion.texttospeech;
import java.util.Locale;
import android.os.Bundle;
import android.app.Activity;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
 EditText type;
 TextToSpeech tts;
 ImageView imgbtn;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  type = (EditText)findViewById(R.id.editText1);
  imgbtn = (ImageView)findViewById(R.id.imageView1);
  imgbtn.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    String random = type.getText().toString();
    tts.speak(random, TextToSpeech.QUEUE_FLUSH, null);
   }
  });
  tts = new TextToSpeech(MainActivity.this, new TextToSpeech.OnInitListener() {
   @Override
   public void onInit(int status) {
    // TODO Auto-generated method stub
    if (status == TextToSpeech.SUCCESS) {
              int result = tts.setLanguage(Locale.US);
              if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
               Toast.makeText(MainActivity.this, "This Language is not supported", Toast.LENGTH_SHORT).show();
              } else {
                  imgbtn.setEnabled(true);
              }
          } else {
           Toast.makeText(MainActivity.this, "Initilization Failed!", Toast.LENGTH_SHORT).show();
          }
   }
  });
 }
 @Override
 protected void onPause() {
  // TODO Auto-generated method stub
  if(tts!=null)
  {
   tts.stop();
   tts.shutdown();
  }
  super.onPause();
 }
}
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 
Changing Pitch Rate
You can set speed pitch level by using setPitch() function. By default the value is 1.0 You can set lower values than 1.0 to decrease pitch level or greater values for increase pitch level.
tts.setPitch(0.6);
Changing Speed Rate
The speed rate can be set using setSpeechRate(). This also will take default of 1.0 value. You can double the speed rate by setting 2.0 or make half the speed level by setting 0.5
tts.setSpeechRate(2);
Changing Pitch Rate
You can set speed pitch level by using setPitch() function. By default the value is 1.0 You can set lower values than 1.0 to decrease pitch level or greater values for increase pitch level.
tts.setPitch(0.6);
Changing Speed Rate
The speed rate can be set using setSpeechRate(). This also will take default of 1.0 value. You can double the speed rate by setting 2.0 or make half the speed level by setting 0.5
tts.setSpeechRate(2);
Download full project from here
Check Android Apps on Google Play
https://play.google.com/store/apps/developer?id=Metro%20App%20Solution&hl=en
 


 
  
 
No comments:
Post a Comment