반응형
|
안드로이드(Android) 아날로그 시계의 시간을 음성(TTS) 으로 알려주기 |
|
개발환경 : window 7 64bit, Eclipse Mars, Android 4.2.2 |
|
이 예제는 아날로그 시간을 만드는 방법과 아날로그 시계에서 바라보는 시간값을 얻어와 TTS 를 이용해 사용자에게 음성으로 시간을 알려주는 예제 입니다. |
먼저 아날로그 시계를 표현하기 위해 레이아웃에는
AnalogClock 추가합니다.
<AnalogClock
android:id="@+id/myAnalogClock"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
소스 구현은 xml 에서 AnalogClock 읽어와 객체를
생성한뒤 setOnClickListener 함수에 바로 아래에
있는 리스너 클래스를 셋팅합니다.
이 리스너 클래스는 시계를 클릭하면 실행하는
코드가 있습니다.
analogClock = (AnalogClock) findViewById(R.id.myAnalogClock); analogClock.setOnClickListener(AnalogClockOnClickListener);
아날로그 시계를 클릭하게 되면 이벤트가 발생하면서
현재 시간을 알아온뒤 문자열을 구성합니다.
그리고 speck() 함수를 이용해 현재 시간을 음성으로
알려 줍니다.
private AnalogClock.OnClickListener AnalogClockOnClickListener
= new AnalogClock.OnClickListener() {
public void onClick(View v) {
final Calendar c = Calendar.getInstance();
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
String myTime = "지금시간은 " + String.valueOf(mHour) + " 시 "
+ String.valueOf(mMinute) + " 분입니다. ";
txtView.setText(myTime);
myTTS.speak(myTime, TextToSpeech.QUEUE_FLUSH, null);
}
};
Activity 객체가 소멸되면 TTS 객체는 소멸시켜야 합니다.
@Override
protected void onDestroy() {
super.onDestroy();
myTTS.shutdown();
}
아래는 메인 Activity 레이아웃의 전체 xml 입니다.
TextView 와 AnalogClock 가 있습니다.
<?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="현재시간 알려주는 TTS" />
<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="시계를 클릭하시면 현재시간을 말해줍니다. " />
<TextView
android:id="@+id/myText"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
메인 Activity 전체 소스입니다.
시계를 클릭하게 되면 현재시간을 말해주고
TextView 에 TTS로 말하는 음성을 문자로
보여줍니다.
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 SampleActivity8 extends Activity implements OnInitListener {
private TextToSpeech myTTS;
private AnalogClock analogClock;
private TextView txtView;
private int mHour, mMinute;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample_activity8);
myTTS = new TextToSpeech(this, this);
analogClock = (AnalogClock) findViewById(R.id.myAnalogClock);
analogClock.setOnClickListener(AnalogClockOnClickListener);
txtView = (TextView) findViewById(R.id.myText);
}
private AnalogClock.OnClickListener AnalogClockOnClickListener
= new AnalogClock.OnClickListener() {
public void onClick(View v) {
final Calendar c = Calendar.getInstance();
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
String myTime = "지금시간은 " + String.valueOf(mHour) + " 시 "
+ String.valueOf(mMinute) + " 분입니다. ";
txtView.setText(myTime);
myTTS.speak(myTime, TextToSpeech.QUEUE_FLUSH, null);
}
};
@Override
protected void onDestroy() {
super.onDestroy();
myTTS.shutdown();
}
public void onInit(int status) {
// TODO Auto-generated method stub
}
}
반응형
'안드로이드 개발' 카테고리의 다른 글
| 안드로이드(Android) 레이아웃에 애니메이션 기능을 이용해 시각효과 주기 (0) | 2014.10.29 |
|---|---|
| 안드로이드(Android) AnimationDrawable 이용하여 애니메이션 만들기 (0) | 2014.10.28 |
| 안드로이드(Android) 스레드를 이용하여 애니메이션 구현하기 (0) | 2014.10.28 |
| 안드로이드(Android) 간단한 스레드 사용법과 숫자값 올리기 (0) | 2014.10.27 |
| 안드로이드(Android) 간단하게 TTS(Text-To-Speech) 구현하기 (8) | 2014.10.26 |
| 안드로이드(Android) WebView 브라우저기능중 URL 이동바 구현 (0) | 2014.10.26 |
| 안드로이드(Android) WebView 이용해 브라우저구현과 메뉴구현 (0) | 2014.10.25 |
| 안드로이드(Android) 스마트 폰에 모든 시스템 정보 알아보기 (1) | 2014.10.25 |

