2010년 7월 13일 화요일

TTS(Text To Speech) 사용하기

음... 반복이의 단어들을 자동으로 읽어주기 위해서 TTS 기능을 추가했다.
약간의 에러가 있어서 해매기는 했지만 지금은 잘 작동이 된다. (음... ㅋㅋ)

TTS 사용을 위한 Sample 코드와 에러를 해결했던 나의 Know-how를 적어 본다.

1. Sample Code (http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/TextToSpeechActivity.html)
 /*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


package com.example.android.apis.app;

import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.example.android.apis.R;

import java.util.Locale;
import java.util.Random;

public class TextToSpeechActivity extends Activity implements TextToSpeech.OnInitListener {

private static final String TAG = "TextToSpeechDemo";

private TextToSpeech mTts;
private Button mAgainButton;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView
(R.layout.text_to_speech);

// Initialize text-to-speech. This is an asynchronous operation.
// The OnInitListener (second argument) is called after initialization completes.
mTts
= new TextToSpeech(this,
this // TextToSpeech.OnInitListener
);

// The button is disabled in the layout.
// It will be enabled upon initialization of the TTS engine.
mAgainButton
= (Button) findViewById(R.id.again_button);

mAgainButton
.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
sayHello
();
}
});
}

@Override
public void onDestroy() {
// Don't forget to shutdown!
if (mTts != null) {
mTts
.stop();
mTts
.shutdown();
}

super.onDestroy();
}

// Implements TextToSpeech.OnInitListener.
public void onInit(int status) {
// status can be either TextToSpeech.SUCCESS or TextToSpeech.ERROR.
if (status == TextToSpeech.SUCCESS) {
// Set preferred language to US english.
// Note that a language may not be available, and the result will indicate this.
int result = mTts.setLanguage(Locale.US);
// Try this someday for some interesting results.
// int result mTts.setLanguage(Locale.FRANCE);
if (result == TextToSpeech.LANG_MISSING_DATA ||
result
== TextToSpeech.LANG_NOT_SUPPORTED) {
// Lanuage data is missing or the language is not supported.
Log.e(TAG, "Language is not available.");
} else {
// Check the documentation for other possible result codes.
// For example, the language may be available for the locale,
// but not for the specified country and variant.

// The TTS engine has been successfully initialized.
// Allow the user to press the button for the app to speak again.
mAgainButton
.setEnabled(true);
// Greet the user.
sayHello
();
}
} else {
// Initialization failed.
Log.e(TAG, "Could not initialize TextToSpeech.");
}
}

private static final Random RANDOM = new Random();
private static final String[] HELLOS = {
"Hello",
"Salutations",
"Greetings",
"Howdy",
"What's crack-a-lackin?",
"That explains the stench!"
};

private void sayHello() {
// Select a random hello.
int helloLength = HELLOS.length;
String hello = HELLOS[RANDOM.nextInt(helloLength)];
mTts
.speak(hello,
TextToSpeech.QUEUE_FLUSH, // Drop all pending entries in the playback queue.
null);
}

}

2. 에러 처리
에러는 "Language is not supported." 이다.
내가 사용하는 폰의 언어 설정은 한국어로 되어 있다.
당연히 지역도 한국으로 되어 있을 것이다.
하지만 Locale.US를 사용하면 지역과 언어가 en_US 가 된다.
따라서 폰의 언어 설정을 영어로 변경하여야 아무런 에러 없이 실행이 된다.
그럼 한국어 설정에서 영어 설정을 사용하려면????
바로 Locale.US => Locale.ENGLISH로 수정하면 된다.
그러면 한국어 설정에서도 TTS에 영어를 적용할 수 있다.

그럼 여기까지 TTS 활용 방법이였다.

모두들 즐거운 하루되세요.

댓글 없음:

댓글 쓰기