사용자와 소통하는 방법 중 하나는 TTS (텍스트 음성 변환) 기술을 사용하는 것입니다. TTS는 Android에 내장되어 있으며 (추가 라이브러리 필요 없음) 오프라인 상태에서도 작동합니다. 이러한 특성으로 인해 TTS는 디스플레이가 없는 모드에서 오류 조건을 처리하는 데 적합합니다. TextToSpeech 클래스를 사용하여 TTS 기능을 참조할 수 있습니다.
TextToSpeech 인스턴스화
AI 안경 활동의 onCreate() 메서드에서 TextToSpeech 클래스를 인스턴스화하여 Activity의 수명 동안 사용할 수 있도록 하는 것이 좋습니다.
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
tts = TextToSpeech(this) { status ->
if(status == TextToSpeech.SUCCESS) {
// Initialization successful
}else {
// Initialization failed
}
}
...
}
TTS가 시작되면 사용자에게 알림
디스플레이가 없는 (오디오 전용) 환경의 경우 onStart() 메서드에서 사용자에게 알림을 보내 앱이 성공적으로 실행되었음을 알립니다.
override fun onStart() {
super.onStart()
tts?.speak("Welcome to Android XR Glasses!",
TextToSpeech.QUEUE_FLUSH,
null,
"welcome_utterance")
...
}
코드에 관한 핵심 사항
TextToSpeech.QUEUE_FLUSH는 텍스트를 즉시 말해야 하며 다른 TTS 발화는 중단해야 함을 나타냅니다.- 이 경우
utteranceId("welcome_utterance")는 이 텍스트의 음성 출력이 완료된 시점을 식별하는 데 사용됩니다. 자세한 내용은UtteranceProgressListener를 참고하세요.
TTS 중단
앱에서 TTS를 중단해야 하는 경우 stop() 메서드를 호출합니다.
// This interrupts the current utterance and discards other utterances in the queue.
tts?.stop()
...
TTS 리소스 정리
활동의 onDestroy() 메서드 내에서 shutdown() 메서드를 호출하여 활동이 소멸될 때 리소스를 정리해야 합니다.
override fun onDestroy() {
super.onDestroy()
tts?.shutdown()
}