준코딩

Android(JAVA) TTS Text to Speech 본문

프로그래밍/Android(JAVA)

Android(JAVA) TTS Text to Speech

Ljunhyeob - App Dev 2023. 3. 29. 10:18

TTS (Text to Speech) 

텍스트 문자를 읽어주는 기능이다.

 

기본적으로 제공해주는 내장 API 를 사용해서 간단하게 만들 수 있다.

 

activity_main.xml 

 

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/txtText"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:inputType="text"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.045"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btnSpeak"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="24dp"
        android:layout_marginLeft="24dp"
        android:text="tts"
        app:layout_constraintBottom_toBottomOf="@+id/txtText"
        app:layout_constraintStart_toEndOf="@+id/txtText"
        app:layout_constraintTop_toTopOf="@+id/txtText"
        app:layout_constraintVertical_bias="0.0" />


</androidx.constraintlayout.widget.ConstraintLayout>

 

MainActivity.java 

package com.example.ttsexample;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

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

import java.util.Locale;


public class MainActivity extends AppCompatActivity {

    private TextToSpeech tts;
    private Button btn_Speak;
    private EditText txtText;


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


        tts = new TextToSpeech(this, new TextToSpeech.OnInitListener(){
            @Override
            public void onInit(int i) {
                if (i != TextToSpeech.ERROR) {
                    tts.setLanguage(Locale.KOREAN);
                }
            }
        });

        btn_Speak = findViewById(R.id.btnSpeak);
        txtText = findViewById(R.id.txtText);

        btn_Speak.setOnClickListener(new View.OnClickListener(){
            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onClick(View view) {
                String text = txtText.getText().toString();
                tts.setPitch(1.0f);
                tts.setSpeechRate(1.0f);
                tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
            }
        });

    }
    @Override public void onDestroy() {
        if (tts != null) {
            tts.stop();
            tts.shutdown();
            tts = null;
        }
        super.onDestroy();
    }
}

 

 

text에 공백 포함 5000자 정도 까지는 정상 작동 테스트 완료 하였습니다.  이 이상은 정상 작동 하는지 확인이 필요합니다.

Comments