Monday, June 8, 2015

// //

How to make a talking clock using text-to-speech

Hi, this time i will teach some easy yet helpfull tutorials to you. Creating a basic analog clock and let the Text-to-Speech to say the Current hour and minute.. activity_main.xml











The MainActivity.java

import java.util.Calendar;

import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.View;
import android.widget.AnalogClock;
import android.widget.TextView;


public class MainActivity extends Activity implements OnInitListener {
 
 TextToSpeech Talktome;
 AnalogClock Clock2;
 TextView ReadText;
 
 private int mHour, mMinute;
 
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
    
      Talktome = new TextToSpeech(this, this);
    
      Clock2 = (AnalogClock)findViewById(R.id.AnalogClock);
      Clock2.setOnClickListener(MyAnalogClockOnClickListener);
      ReadText = (TextView)findViewById(R.id.Text2read);
    
  }

 @Override
 public void onInit(int status) {
  // TODO Auto-generated method stub
  
 }

 @Override
 protected void onDestroy() {
  // TODO Auto-generated method stub
  super.onDestroy();
  Talktome.shutdown();
 }
 
 private AnalogClock.OnClickListener MyAnalogClockOnClickListener
  = new AnalogClock.OnClickListener(){

  @Override
  public void onClick(View v) {
   // TODO Auto-generated method stub
   
   final Calendar c = Calendar.getInstance();
   
   mHour = c.get(Calendar.HOUR_OF_DAY);
   mMinute = c.get(Calendar.MINUTE);

   
   String myTime= "The Time is  is "
             + String.valueOf(mHour)
             + " Hour "
             +String.valueOf(mMinute)
             + " Minute";
   
   ReadText.setText(myTime);
      Talktome.speak(myTime, TextToSpeech.QUEUE_FLUSH, null);
  }
 };
}
I know your quite good programmers so i thought its easy to understand, right?