Search in sources :

Example 1 with KiwixTextToSpeech

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());
}
Also used : KiwixTextToSpeech(org.kiwix.kiwixmobile.utils.KiwixTextToSpeech) ImageView(android.widget.ImageView) ToolbarScrollingKiwixWebView(org.kiwix.kiwixmobile.views.web.ToolbarScrollingKiwixWebView) RecyclerView(android.support.v7.widget.RecyclerView) BindView(butterknife.BindView) ActionMenuItemView(android.support.v7.view.menu.ActionMenuItemView) View(android.view.View) WebView(android.webkit.WebView) KiwixWebView(org.kiwix.kiwixmobile.views.web.KiwixWebView) ToolbarStaticKiwixWebView(org.kiwix.kiwixmobile.views.web.ToolbarStaticKiwixWebView)

Aggregations

ActionMenuItemView (android.support.v7.view.menu.ActionMenuItemView)1 RecyclerView (android.support.v7.widget.RecyclerView)1 View (android.view.View)1 WebView (android.webkit.WebView)1 ImageView (android.widget.ImageView)1 BindView (butterknife.BindView)1 KiwixTextToSpeech (org.kiwix.kiwixmobile.utils.KiwixTextToSpeech)1 KiwixWebView (org.kiwix.kiwixmobile.views.web.KiwixWebView)1 ToolbarScrollingKiwixWebView (org.kiwix.kiwixmobile.views.web.ToolbarScrollingKiwixWebView)1 ToolbarStaticKiwixWebView (org.kiwix.kiwixmobile.views.web.ToolbarStaticKiwixWebView)1