Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 1차원 DP
- 2차원 dp
- 99클럽
- @BeforeAll
- @BeforeEach
- @Builder
- @Entity
- @GeneratedValue
- @GenericGenerator
- @NoargsConstructor
- @Query
- @Table
- @Transactional
- Actions
- Amazon EFS
- amazon fsx
- Android Studio
- ANSI SQL
- ApplicationEvent
- assertThat
- async/await
- AVG
- AWS
- Azure
- bind
- builder
- button
- c++
- c++ builder
- c03
Archives
- Today
- Total
기록
androidStudio/java/TTS 사용하기 본문
코드 작성하기
1. manifests에 action추가
<manifest>...
<queries>
<intent>
<action android:name="android.intent.action.TTS_SERVICE"/>
</intent>
</queries>
<application .../>
</manifest>
2. xml에 버튼 추가
<ImageButton
android:id="@+id/imageButton_speak"
android:layout_width="30dp"
android:layout_height="30dp"
app:layout_constraintEnd_toStartOf="@+id/memu" />
3. code 추가
import static android.speech.tts.TextToSpeech.ERROR;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
ImageButton bt_qrcode;
private TextToSpeech tts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt_qrcode = findViewById(R.id.main_ImageButton_qrcode);
bt_qrcode.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
speak("테스트");
}
});
}
private void speak(String text) {
tts = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status != ERROR){
int result = tts.setLanguage(Locale.KOREA); // 언어 선택
if(result == TextToSpeech.LANG_NOT_SUPPORTED || result == TextToSpeech.LANG_MISSING_DATA){
Log.e("TTS", "This Language is not supported");
}else{
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null, null);
}
}else{
Log.e("TTS", "Initialization Failed!");
}
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
if(tts!=null){ // 사용한 TTS객체 제거
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
}
4. 오류
다른 블로그를 참고하여 작성하다가 texttospeech: speak failed: not bound to tts engine 오류가 났다. TTS가 완전히 초기화되기 전에 tts를 사용했기 때문이라고 했다. tts를 사용할 때마다 새롭게 생성하도록 해서 문제를 해결했다.
// 실패한 코드
private void speak(String text) {
tts = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status != ERROR){
int result = tts.setLanguage(Locale.KOREA); // 언어 선택
if(result == TextToSpeech.LANG_NOT_SUPPORTED || result == TextToSpeech.LANG_MISSING_DATA){
Log.e("TTS", "This Language is not supported");
}
}else{
Log.e("TTS", "Initialization Failed!");
}
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null, null);
}
});
}
manifests에 action추가하지 않았더니, state 변수에 ERROR 값이 저장되었다. 1번처럼 작성 후 제대로 작동했다.
결과
참고자료
https://developer.android.com/reference/android/speech/tts/TextToSpeech
'Moblie > Android' 카테고리의 다른 글
androidStudio/java/naver map api 사용하기 (0) | 2022.01.22 |
---|---|
[issue] java.lang.SecurityException (0) | 2021.12.07 |
androidStudio/java/FireBase에서 특정 데이터만 가져오기 (0) | 2021.11.30 |
androidStudio/Google Map API 사용하기 (0) | 2021.11.28 |
androidStudio/localhost 서버 접속하기 (0) | 2021.06.07 |
Comments