Tuesday, October 6, 2009

Talking AlanlogClock, using Text-To-Speech function.

Extends from previous article; Android AnalogClock and AndroidSpeech: Android can speech! this exercise implement a analog clock with speech function. When you click on the analog clock, the current hour and minute will be speeched.

Create a basic Analog Clock application following the article Android AnalogClock, with the target platform set Android 1.6, because we have to use the Test-To-Speech function.

Add TextView in main.xml, it's helpful for checking.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<AnalogClock
android:id="@+id/myAnalogClock"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="I can speech"
/>
<TextView
android:id="@+id/myText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>

</LinearLayout>

main.xml can be downloaded here.

Implement AnalogClock.OnClickListener to start the speech once the AnalogClick is clicked. In AnalogClock.OnClickListener, the current time can be retrieve by Calendar.getInstance().

AndroidClock.java
package com.exercise.AndroidClock;

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 AndroidClock extends Activity implements OnInitListener {

TextToSpeech myTTS;
AnalogClock MyAnalogClock;
TextView MyText;

private int mHour, mMinute;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

myTTS = new TextToSpeech(this, this);

MyAnalogClock = (AnalogClock)findViewById(R.id.myAnalogClock);
MyAnalogClock.setOnClickListener(MyAnalogClockOnClickListener);
MyText = (TextView)findViewById(R.id.myText);

}

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

}

@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
myTTS.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= "Now is "
+ String.valueOf(mHour)
+ " Hour "
+String.valueOf(mMinute)
+ " Minute";

MyText.setText(myTime);
myTTS.speak(myTime, TextToSpeech.QUEUE_FLUSH, null);
}
};
}

AndroidClock.java can be downloaded here.

No comments:

Post a Comment