Android TTS(TextToSpeech) 음성출력 사용하기
<?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"/>
<Button
android:id="@+id/button02"
android:layout_width="match_parent"
android:layout_height="wrap_content"
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="wrap_content"
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="wrap_content"
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="wrap_content"
android:layout_marginTop="20dp"
android:text="TTS 목소리 속도 설정 느리게 [tts.setSpeechRate(0.5f)]"
android:textColor="#000000"
android:textSize="18dp"/>
</LinearLayout>
package com.jwlee.tts_example;
import androidx.appcompat.app.AppCompatActivity;
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;
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;
@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.setPitch(1.0f); // 음성 톤을 기본 설정
tts.setSpeechRate(1.0f); // 읽는 속도는 기본 설정
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;
}
}
}
'기타 ETC > Android Studio' 카테고리의 다른 글
안드로이드 애드몹 보상형 광고 간단하게 만들어 보기 (0) | 2020.11.25 |
---|---|
주간 달력 만들기 (0) | 2020.10.11 |
Android SQLite Error: android.database.sqlite.SQLiteException: no such column: (code 1) (0) | 2020.07.15 |
COVID19 TRACKER Android source code (0) | 2020.07.14 |
안드로이드 뮤직플레이어 소스코드 (0) | 2020.07.13 |