hyeals study
구글 TTS API를 안드로이드에서 써보기! 본문
TTS란?
Text To Speech의 약자로 텍스트를 음성으로 출력해주는 기술이다.
(안드로이드에서는 무료로 제공해준다. 구글 TTS를 내장 API로 지원해주고 있다!)
물론 반대인 STT(Speech To Text)로 지원해준다.
안드로이드에서 TTS 적용해보기
activicy_main.xml
<?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:layout_marginTop="30dp"
android:inputType="textPersonName" />
<Button
android:id="@+id/button"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="음성 출력" />
</LinearLayout>
MainActivity.java
package com.example.tts_test;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Build;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.util.Locale;
public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener {
private TextToSpeech tts;
private Button speak_out;
private EditText input_text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tts = new TextToSpeech(this, this);
speak_out = findViewById(R.id.button);
input_text = findViewById(R.id.editText);
speak_out.setOnClickListener(new View.OnClickListener(){
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) // LOLLIPOP이상 버전에서만 실행 가능
@Override
public void onClick(View v){
speakOut();
}
});
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void speakOut(){
CharSequence text = input_text.getText();
tts.setPitch((float)0.6); // 음성 톤 높이 지정
tts.setSpeechRate((float)0.1); // 음성 속도 지정
// 첫 번째 매개변수: 음성 출력을 할 텍스트
// 두 번째 매개변수: 1. TextToSpeech.QUEUE_FLUSH - 진행중인 음성 출력을 끊고 이번 TTS의 음성 출력
// 2. TextToSpeech.QUEUE_ADD - 진행중인 음성 출력이 끝난 후에 이번 TTS의 음성 출력
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null, "id1");
}
@Override
public void onDestroy() {
if(tts!=null){ // 사용한 TTS객체 제거
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void onInit(int status) { // OnInitListener를 통해서 TTS 초기화
if(status == TextToSpeech.SUCCESS){
int result = tts.setLanguage(Locale.KOREA); // TTS언어 한국어로 설정
if(result == TextToSpeech.LANG_NOT_SUPPORTED || result == TextToSpeech.LANG_MISSING_DATA){
Log.e("TTS", "This Language is not supported");
}else{
speak_out.setEnabled(true);
speakOut();// onInit에 음성출력할 텍스트를 넣어줌
}
}else{
Log.e("TTS", "Initialization Failed!");
}
}
}
테스트 해보기!
잘된다~
developer.android.com/reference/android/speech/tts/TextToSpeech
TextToSpeech | Android 개발자 | Android Developers
developer.android.com
ebbnflow.tistory.com/188#recentComments
[Android] 구글STT, TTS 사용하기 (android.speech)
● Android Speech Recognizer 안드로이드에서는 구글 SpeechToText, TextToSpeech 기술인 stt, tts를 내장 API로 지원하고 있습니다. 별도의 설치 없이 gradle에 특별한 세팅이 필요하지 않고 manifest에 몇 가..
ebbnflow.tistory.com
github.com/sdsb8432/TextToSpeech-Android/blob/master/README.md
sdsb8432/TextToSpeech-Android
Text to Speech for Android Application with Google API - sdsb8432/TextToSpeech-Android
github.com
'안드로이드' 카테고리의 다른 글
구글 TTS API를 안드로이드에서 써보기!(2??) (1) | 2021.02.14 |
---|---|
Rxjava를 안드로이드에 적용시켜보기! (0) | 2021.02.08 |
RxJava 사용해보기 (0) | 2021.02.07 |
RxJava??? (0) | 2021.02.06 |
웹툰 바로가기 앱 (0) | 2020.05.24 |
Comments