I have been trying from so long to highlight the text been spoken by text to speech, and yeah I have done it with this callback from text to speech class :

 @Override
        public void onRangeStart(String utteranceId, int start,
                                 int end, int frame) {
           runOnUiThread(() -> {
                set_span(start, end);//I use this to highlight
                Log.wtf(TAG, String.valueOf(start + " " + end) + " " + utteranceId);
            });
        }

 public void set_span(int start, int end) {
    SpannableStringBuilder builder =
            new SpannableStringBuilder(main_input.getText().toString());
    builder.setSpan(new ForegroundColorSpan(Color.RED), start, end, 0);
    main_input.setText(builder, EditText.BufferType.SPANNABLE);
    main_input.setSelection(start);
}

however, if I stop text to speech while it is speaking then the problem arises : the value I get from onRangeStart that is start and end falls back to 0 and causes unexpected behavior while setting the span next time if I press resume.

So, I want to save the current onRangestart callback values start and end and continue to span from the previous position.

0 Answers