Search in sources :

Example 1 with UtteranceProgressListener

use of android.speech.tts.UtteranceProgressListener in project speechutils by Kaljurand.

the class TtsProvider method say.

@SuppressLint("NewApi")
public void say(String text) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
        mTts.setOnUtteranceProgressListener(new UtteranceProgressListener() {

            @Override
            public void onDone(String utteranceId) {
                mAudioPauser.resume();
            }

            @Override
            public void onError(String utteranceId) {
                mAudioPauser.resume();
            }

            @Override
            public void onStart(String utteranceId) {
            }
        });
    } else {
        mTts.setOnUtteranceCompletedListener(new TextToSpeech.OnUtteranceCompletedListener() {

            @Override
            public void onUtteranceCompleted(String utteranceId) {
                mAudioPauser.resume();
            }
        });
    }
    HashMap<String, String> params = new HashMap<String, String>();
    params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, UTT_COMPLETED_FEEDBACK);
    mAudioPauser.pause();
    mTts.speak(text, TextToSpeech.QUEUE_FLUSH, params);
}
Also used : HashMap(java.util.HashMap) UtteranceProgressListener(android.speech.tts.UtteranceProgressListener) TextToSpeech(android.speech.tts.TextToSpeech) SuppressLint(android.annotation.SuppressLint)

Example 2 with UtteranceProgressListener

use of android.speech.tts.UtteranceProgressListener in project speechutils by Kaljurand.

the class TtsProvider method say.

@SuppressLint("NewApi")
public void say(String text, final Listener listener) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
        mTts.setOnUtteranceProgressListener(new UtteranceProgressListener() {

            @Override
            public void onDone(String utteranceId) {
                mAudioPauser.resume();
                listener.onDone();
            }

            @Override
            public void onError(String utteranceId) {
                mAudioPauser.resume();
            }

            @Override
            public void onStart(String utteranceId) {
            }
        });
    } else {
        mTts.setOnUtteranceCompletedListener(new TextToSpeech.OnUtteranceCompletedListener() {

            @Override
            public void onUtteranceCompleted(String utteranceId) {
                mAudioPauser.resume();
                listener.onDone();
            }
        });
    }
    HashMap<String, String> params = new HashMap<String, String>();
    params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, UTT_COMPLETED_FEEDBACK);
    mAudioPauser.pause();
    mTts.speak(text, TextToSpeech.QUEUE_FLUSH, params);
}
Also used : HashMap(java.util.HashMap) UtteranceProgressListener(android.speech.tts.UtteranceProgressListener) TextToSpeech(android.speech.tts.TextToSpeech) SuppressLint(android.annotation.SuppressLint)

Example 3 with UtteranceProgressListener

use of android.speech.tts.UtteranceProgressListener in project LibreraReader by foobnix.

the class TTSService method playPage.

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)
private void playPage(String preText, int pageNumber, String anchor) {
    if (pageNumber != -1) {
        EventBus.getDefault().post(new MessagePageNumber(pageNumber));
        AppState.get().lastBookPage = pageNumber;
        CodecDocument dc = getDC();
        if (dc == null) {
            LOG.d(TAG, "CodecDocument", "is NULL");
            return;
        }
        int pageCount = dc.getPageCount();
        LOG.d(TAG, "CodecDocument PageCount", pageNumber, pageCount);
        if (pageNumber >= pageCount) {
            TempHolder.get().timerFinishTime = 0;
            Vibro.vibrate(1000);
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
            }
            TTSEngine.get().getTTS().setOnUtteranceCompletedListener(null);
            TTSEngine.get().speek(LibreraApp.context.getString(R.string.the_book_is_over));
            EventBus.getDefault().post(new TtsStatus());
            return;
        }
        CodecPage page = dc.getPage(pageNumber);
        String pageHTML = page.getPageHTML();
        page.recycle();
        pageHTML = TxtUtils.replaceHTMLforTTS(pageHTML);
        if (TxtUtils.isNotEmpty(anchor)) {
            int indexOf = pageHTML.indexOf(anchor);
            if (indexOf > 0) {
                pageHTML = pageHTML.substring(indexOf);
                LOG.d("find anchor new text", pageHTML);
            }
        }
        LOG.d(TAG, pageHTML);
        if (TxtUtils.isEmpty(pageHTML)) {
            LOG.d("empty page play next one", emptyPageCount);
            emptyPageCount++;
            if (emptyPageCount < 3) {
                playPage("", AppState.get().lastBookPage + 1, null);
            }
            return;
        }
        emptyPageCount = 0;
        String[] parts = TxtUtils.getParts(pageHTML);
        String firstPart = parts[0];
        final String secondPart = parts[1];
        if (TxtUtils.isNotEmpty(preText)) {
            preText = TxtUtils.replaceLast(preText, "-", "");
            firstPart = preText + firstPart;
        }
        if (Build.VERSION.SDK_INT >= 15) {
            TTSEngine.get().getTTS().setOnUtteranceProgressListener(new UtteranceProgressListener() {

                @Override
                public void onStart(String utteranceId) {
                }

                @Override
                public void onError(String utteranceId) {
                    TTSEngine.get().stop();
                }

                @Override
                public void onDone(String utteranceId) {
                    LOG.d(TAG, "onUtteranceCompleted");
                    if (TempHolder.get().timerFinishTime != 0 && System.currentTimeMillis() > TempHolder.get().timerFinishTime) {
                        LOG.d(TAG, "Timer");
                        TempHolder.get().timerFinishTime = 0;
                        return;
                    }
                    playPage(secondPart, AppState.get().lastBookPage + 1, null);
                    SettingsManager.updateTempPage(AppState.get().lastBookPath, AppState.get().lastBookPage + 1);
                }
            });
        } else {
            TTSEngine.get().getTTS().setOnUtteranceCompletedListener(new OnUtteranceCompletedListener() {

                @Override
                public void onUtteranceCompleted(String utteranceId) {
                    LOG.d(TAG, "onUtteranceCompleted");
                    if (TempHolder.get().timerFinishTime != 0 && System.currentTimeMillis() > TempHolder.get().timerFinishTime) {
                        LOG.d(TAG, "Timer");
                        TempHolder.get().timerFinishTime = 0;
                        return;
                    }
                    playPage(secondPart, AppState.get().lastBookPage + 1, null);
                    SettingsManager.updateTempPage(AppState.get().lastBookPath, AppState.get().lastBookPage + 1);
                }
            });
        }
        TTSNotification.show(AppState.get().lastBookPath, pageNumber + 1);
        TTSEngine.get().speek(firstPart);
        EventBus.getDefault().post(new TtsStatus());
        savePage();
    }
}
Also used : OnUtteranceCompletedListener(android.speech.tts.TextToSpeech.OnUtteranceCompletedListener) UtteranceProgressListener(android.speech.tts.UtteranceProgressListener) CodecPage(org.ebookdroid.core.codec.CodecPage) CodecDocument(org.ebookdroid.core.codec.CodecDocument) TargetApi(android.annotation.TargetApi)

Aggregations

UtteranceProgressListener (android.speech.tts.UtteranceProgressListener)3 SuppressLint (android.annotation.SuppressLint)2 TextToSpeech (android.speech.tts.TextToSpeech)2 HashMap (java.util.HashMap)2 TargetApi (android.annotation.TargetApi)1 OnUtteranceCompletedListener (android.speech.tts.TextToSpeech.OnUtteranceCompletedListener)1 CodecDocument (org.ebookdroid.core.codec.CodecDocument)1 CodecPage (org.ebookdroid.core.codec.CodecPage)1