사용한 툴 : 안드로이드 스튜디오

사용 최소 버전 : API 18


TTS란

Text To Speech 의 약자이며 텍스트를 음성으로 읽어주는 기능을 말한다.

사용할 땐 클래스 내부에 TextToSpeech 객체를 생성하고, 객체 생성시 이벤트를 처리할 TextToSpeech.OnInitListener를 implement 한다.

객체 생성 까지 약간의 시간이 걸리며 이 시간동안은 TTS기능을 사용 할 수 없다.

객체 생성이 끝나면 내부 설정(TTS 엔진, 언어, 음성 속도 같은 set 계열과 get 계열) 메소드를 사용 할 수 있다.

자세한 것은 아래 링크에서 참고한다.


http://developer.android.com/reference/android/speech/tts/TextToSpeech.html#QUEUE_ADD

TTS 를 사용하기 전 디바이스에서 사용 할 수 있도록 설정해주어야 한다.

설정은 아래 그림을 참고하면 된다 (API22기준)


사용하기 전 TTS 설치 및 설정



Simple Code

디바이스에 한국어 설정이 되있다고 가정했을 때,

어플을 실행하고 잠시뒤 'TTS기능 테스트 중'이라고 말한다.


 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener{
 
    private TextToSpeech ttsClient;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_etcbeacon);
 
        ttsClient= new TextToSpeech(getApplicationContext(),this);
    }
    @Override
    public void onInit(int i) {
        ttsClient.speak("TTS기능 테스트 중.",TextToSpeech.QUEUE_FLUSH,null);
    }
 
}
 
cs

▲MainActivity.java

+ Recent posts