iOS를 사랑하는 AOS 개발자

[Android] 안드로이드 TTS(Text to Speech) 사용법 본문

Android ( JAVA )/개발

[Android] 안드로이드 TTS(Text to Speech) 사용법

아사안개 2022. 11. 20. 16:14
반응형
SMALL

👼🏻 초보 안드로이드 개발자가 매번 구글링하기 싫어서 정리하는 블로그 👼🏻 

 

안녕하세요! 🙋🏻‍♀️

TTS (Text to Speech) 기능을 살펴볼거에요!!

TTS는 말 그대로 글씨를 목소리로 읽어주는 기능을 뜻하는 줄임말인데요

보통 시각장애인분들을 위한 기능으로 사용하는게 대부분이죠!

하지만 필요에 의해 일반 사용자들에게도 제공되는 기능입니다 😸

바로 시작해보겠습니다!

언제나 그랫듯이 View 부터 그려봅니다!

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

    <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="wrap_content"
        android:layout_marginTop="20dp"
        android:text="TTS 실행"
        android:textColor="#000000"
        android:textSize="18dp"/>

</LinearLayout>

 

사용자가 EditText 에 텍스트를 입력 후 버튼을 클릭하면 읽어주는 식으로 한번 해보겠습니다

 

package com.ipageon.tts_test;

import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import static android.speech.tts.TextToSpeech.ERROR;

import androidx.appcompat.app.AppCompatActivity;

import java.util.Locale;

public class MainActivity extends AppCompatActivity {

    private TextToSpeech tts;              // TTS 변수 선언
    private EditText editText;
    private Button button01;
    private final String TTS_ID = "TTS";

    @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);


        // 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(), TextToSpeech.QUEUE_FLUSH, null, TTS_ID);
            }
        });

    }

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

 

TextToSpeech 라는 객체로 인해 너무나도 쉽고 간단하게 기능을 구현해보았는데요,

사실 그냥 읽어주기 기능 외에도

Media Volume 에 관계없이 목소리 Tone을 높여주거나 내려주거나, 읽는 속도를 빠르게 혹은 느리게 해준다거나

기능이 너무너무 다양함으로 자세한 사항은 Android Developer 사이트에서 참고해서 많은 기능을 구현해보는것도 좋을 것 같습니다 :)

 

반응형

 

틀린부분이 있거나, 궁금하신게 있거나, 그냥 아무말이나 하고싶으면 댓글 남겨주세요 🥴

봐주셔서 감사합니다 🥰

반응형
LIST
Comments