내 앱에서 텍스트를 말하기 위해 TextToSpeech
클래스를 사용하려고합니다. 코드를 실행할 때 아무 것도 듣지 못하면 볼륨이 높습니다. 내 코드에 어떤 문제가 있습니까? 허가가 필요합니까?Android text to speech
public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener {
TextToSpeech textToSpeech;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textToSpeech = new TextToSpeech(this, this);
speakOut();
}
@Override
public void onInit(int Text2SpeechCurrentStatus) {
if (Text2SpeechCurrentStatus == TextToSpeech.SUCCESS) {
int result = textToSpeech.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not supported");
} else {
speakOut();
}
} else {
Log.e("TTS", "Initilization Failed!");
}
}
private void speakOut() {
String g= "Hello";
textToSpeech.speak(g, TextToSpeech.QUEUE_FLUSH, null);
}
}