Monday 23 December 2013

Android-Text-to-Speech-example

Android Text to speech Example

Hi Friend
  Today we explain how to create Android Narrator.
Android provide text to speech class. using this class we can easily create an message reader type application, using the method of this class we can control speed of narrating and sound pitch also.

Java source Code for Narrator:


package in.androidshivendra.texttospeechexample;

import java.util.Locale;

import android.os.Bundle;
import android.app.Activity;
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.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Spinner;

public class MainActivity extends Activity implements
TextToSpeech.OnInitListener{
   String speed[] = {  "Very Slow", "Slow", "Normal", "Fast", "Very Fast"};
   Float speedf[] = {(float) 0.1,(float) 0.5,(float) 1.0,(float) 1.5,(float) 2.0};
   Spinner spspeed;
   Button btn;
   EditText emsg;
   RadioGroup rg;
   TextToSpeech tts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter adp = new ArrayAdapter(MainActivity.this, android.R.layout.simple_spinner_item,speed);
 
spspeed = (Spinner)findViewById(R.id.spinner1);
btn = (Button)findViewById(R.id.button1);
emsg = (EditText)findViewById(R.id.editText1);
rg = (RadioGroup)findViewById(R.id.radioGroup1);
tts = new TextToSpeech(this,this);
spspeed.setAdapter(adp);
spspeed.setOnItemSelectedListener(new OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
tts.setSpeechRate(speedf[arg2]);
}

@Override
public void onNothingSelected(AdapterView arg0) {
// TODO Auto-generated method stub
tts.setSpeechRate((float)1.0);
}
});
rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
RadioButton rb = (RadioButton)findViewById(checkedId);
if(rb.isChecked())
{
String gen = rb.getText().toString();
if(gen.equals("Male"))
tts.setPitch(1.0f);
else
tts.setPitch(2.5f);
}
}
});
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String text = emsg.getText().toString();
    tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public void onInit(int status) {
// TODO Auto-generated method stub
if (status == TextToSpeech.SUCCESS) {

   int result = tts.setLanguage(Locale.ENGLISH);

   if (result == TextToSpeech.LANG_MISSING_DATA
           || result == TextToSpeech.LANG_NOT_SUPPORTED) {
       Log.e("TTS", "This Language is not supported");
   } else {
    btn.setEnabled(true);
   
   }

} else {
   Log.e("TTS", "Initilization Failed!");
}
}
@Override
public void onDestroy() {
// Don't forget to shutdown tts!
if (tts != null) {
   tts.stop();
   tts.shutdown();
}
super.onDestroy();
}

}

Layout xml code:-



    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    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:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textMultiLine" >

       
   

   



You can download Text- to - speech- example here


No comments:

Today's Pageviews