일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- IOS
- Android Studio
- xocde
- 백준
- customPopup
- 링크드리스트
- Xcode
- label
- Firebase
- 준코딩
- android java
- C언어
- TextField
- 커스텀팝업
- Swift
- 플러터
- 예외처리
- Android
- swift baekjoon
- BAEKJOON
- deeplink
- text to speech
- 보호와 보안
- storyboard
- 버블정렬
- 안드로이드스튜디오
- 연결리스트
- FLUTTER
- 자바
- 안드로이드
Archives
- Today
- Total
준코딩
Android(JAVA) TTS Text to Speech 본문
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자 정도 까지는 정상 작동 테스트 완료 하였습니다. 이 이상은 정상 작동 하는지 확인이 필요합니다.
'프로그래밍 > Android(JAVA)' 카테고리의 다른 글
Android(java) 앱 버전 업데이트 빌드 에러[The destination foler does not exist or is not writeable] (2) | 2023.04.05 |
---|---|
Firebase SDK 연동하기 (Android Java) (0) | 2023.03.30 |
1.딥링크 (DeepLink) 알아보기. (0) | 2021.08.12 |
리스트(토스트,종료 버튼) (0) | 2019.03.21 |
계산기 (0) | 2019.03.21 |
Comments