use of org.kiwix.kiwixmobile.utils.KiwixTextToSpeech in project kiwix-android by kiwix.
the class KiwixMobileActivity method setUpTTS.
private void setUpTTS() {
tts = new KiwixTextToSpeech(this, () -> {
if (menu != null) {
menu.findItem(R.id.menu_read_aloud).setVisible(true);
}
}, new KiwixTextToSpeech.OnSpeakingListener() {
@Override
public void onSpeakingStarted() {
isSpeaking = true;
runOnUiThread(() -> {
menu.findItem(R.id.menu_read_aloud).setTitle(createMenuItem(getResources().getString(R.string.menu_read_aloud_stop)));
TTSControls.setVisibility(View.VISIBLE);
});
}
@Override
public void onSpeakingEnded() {
isSpeaking = false;
runOnUiThread(() -> {
menu.findItem(R.id.menu_read_aloud).setTitle(createMenuItem(getResources().getString(R.string.menu_read_aloud)));
TTSControls.setVisibility(View.GONE);
pauseTTSButton.setText(R.string.tts_pause);
});
}
}, new AudioManager.OnAudioFocusChangeListener() {
@Override
public void onAudioFocusChange(int focusChange) {
Log.d(TAG_KIWIX, "Focus change: " + String.valueOf(focusChange));
if (tts.currentTTSTask == null) {
tts.stop();
return;
}
switch(focusChange) {
case (AudioManager.AUDIOFOCUS_LOSS):
if (!tts.currentTTSTask.paused)
tts.pauseOrResume();
pauseTTSButton.setText(R.string.tts_resume);
break;
case (AudioManager.AUDIOFOCUS_GAIN):
pauseTTSButton.setText(R.string.tts_pause);
break;
}
}
});
pauseTTSButton.setOnClickListener(view -> {
if (tts.currentTTSTask == null) {
tts.stop();
return;
}
if (tts.currentTTSTask.paused) {
tts.pauseOrResume();
pauseTTSButton.setText(R.string.tts_pause);
} else {
tts.pauseOrResume();
pauseTTSButton.setText(R.string.tts_resume);
}
});
stopTTSButton.setOnClickListener((View view) -> tts.stop());
}
Aggregations