프로그래밍/안드로이드

[안드로이드] TTS(TextToSpeech) 음성출력 사용하기

오치리일상 2017. 8. 26.

TTS (Text To Speech)

 

안드로이드 기능중에 텍스트를 읽어주는 TTS(Text To Speech)라는 특별한 기능이 있다.

 

이 기능으로 인해서 따로 성우의 목소리로 녹음을 하지 않아도 텍스트 작업으로만으로도 음성 출력 기능을 사용할 수 있다.

 

 

 

사용 방법은 간단하다. 

 

1. TTS 객체를 생성한다.

 

2. TTS를 초기화 한다.

 

3. 원하는 문장을 음성 출력한다.

 

4. 사용 후 객체를 제거한다.

 

 

 

 

 

 

위 순서대로 코드를 만들어 본다.

 

* activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="텍스트를 입력하세요."
        />
    <Button
        android:id="@+id/button01"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginTop="20dp"
        android:text="TTS 실행"
        android:textColor="#000000"
        android:textSize="18dp"/>
    <Button
        android:id="@+id/button02"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginTop="20dp"
        android:text="TTS 목소리 톤 설정 높게 [tts.setPitch(1.5f)]"
        android:textColor="#000000"
        android:textSize="18dp"/>
    <Button
        android:id="@+id/button03"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginTop="20dp"
        android:text="TTS 목소리 톤 설정 낮게 [tts.setPitch(0.5f)]"
        android:textColor="#000000"
        android:textSize="18dp"/>
    <Button
        android:id="@+id/button04"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginTop="20dp"
        android:text="TTS 목소리 속도 설정 빠르게 [tts.setSpeechRate(1.5f)]"
        android:textColor="#000000"
        android:textSize="18dp"/>
    <Button
        android:id="@+id/button05"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginTop="20dp"
        android:text="TTS 목소리 속도 설정 느리게 [tts.setSpeechRate(0.5f)]"
        android:textColor="#000000"
        android:textSize="18dp"/>

</LinearLayout>

 

위와 같이 테스트할 레이아웃을 만들고 자바코드 작업을 한다.

 

 

 

 

 

* MainActivity.java

package com.studio572.sampletts;

import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.util.Locale;

import static android.speech.tts.TextToSpeech.ERROR;

public class MainActivity extends AppCompatActivity {

    private TextToSpeech tts;              // TTS 변수 선언
    private EditText editText;
    private Button button01, button02, button03, button04, button05;

    public MainActivity() {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        editText = (EditText) findViewById(R.id.editText);
        button01 = (Button) findViewById(R.id.button01);
        button02 = (Button) findViewById(R.id.button02);
        button03 = (Button) findViewById(R.id.button03);
        button04 = (Button) findViewById(R.id.button04);
        button05 = (Button) findViewById(R.id.button05);


        // TTS를 생성하고 OnInitListener로 초기화 한다.
        tts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if(status != ERROR) {
                    // 언어를 선택한다.
                    tts.setLanguage(Locale.KOREAN);
                }
            }
        });
        button01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // editText에 있는 문장을 읽는다.
                tts.speak(editText.getText().toString(),TextToSpeech.QUEUE_FLUSH, null);
            }
        });
        button02.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tts.setPitch(2.0f);         // 음성 톤을 2.0배 올려준다.
                tts.setSpeechRate(1.0f);    // 읽는 속도는 기본 설정
                // editText에 있는 문장을 읽는다.
                tts.speak(editText.getText().toString(),TextToSpeech.QUEUE_FLUSH, null);
            }
        });
        button03.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tts.setPitch(0.5f);         // 음성 톤을 0.5배 내려준다.
                tts.setSpeechRate(1.0f);    // 읽는 속도는 기본 설정
                // editText에 있는 문장을 읽는다.
                tts.speak(editText.getText().toString(),TextToSpeech.QUEUE_FLUSH, null);
            }
        });
        button04.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tts.setPitch(1.0f);         // 음성 톤은 기본 설정
                tts.setSpeechRate(2.0f);    // 읽는 속도를 2배 빠르기로 설정
                // editText에 있는 문장을 읽는다.
                tts.speak(editText.getText().toString(),TextToSpeech.QUEUE_FLUSH, null);
            }
        });
        button05.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tts.setPitch(1.0f);         // 음성 톤은 기본 설정
                tts.setSpeechRate(0.5f);    // 읽는 속도를 0.5빠르기로 설정
                // editText에 있는 문장을 읽는다.
                tts.speak(editText.getText().toString(),TextToSpeech.QUEUE_FLUSH, null);
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // TTS 객체가 남아있다면 실행을 중지하고 메모리에서 제거한다.
        if(tts != null){
            tts.stop();
            tts.shutdown();
            tts = null;
        }
    }
}

 

* TTS.speak() 메소드의 매개변수에 대해 알아본다.

 

- 번째 매개변수 : 음성 출력할 문장을 넘겨준다.

 

- 두번째 매개변수 : TextToSpeech.QUEUE_FLUSH(진행중인 음성 출력을 끊고 이번 TTS의 음성 출력을 한다). TextToSpeech.QUEUE_ADD(진행중인 음성 출력이 끝난 후에 이번 TTS의 음성 출력을 진행한다.)

 

- 세번째 매개변수 : null을 넘겨준다.

 

 

 

 

* TTS의 속성값

 

- TTS.setPitch(float pitch) : 음성 톤 높이 설정

 

- TTS.setSpeechRate(float speechRate) : 읽는 속도 설정

 

- TTS.setlanguage(Locale loc) : 언어 선택

 

 

 

 

 

 

댓글