Search code examples
androidandroid-fragmentstext-to-speech

TTS is not reading the text of proper fragment


I guess I'll fail to explain the issue because it is WEIRD.

I need an app similar to online tutorials,the basic functionality is to switch among the pages(fragments in my case) and the content should read out.

So, I've followed Mr.Tamada " - The life saviour" tutorials The TTS and Fragments and I've merged them both.

Motto is to read the data in fragments.

Code:

public class TopRatedFragment extends Fragment implements TextToSpeech.OnInitListener
{
    private static TextToSpeech tts;
    TextView tv;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_top_rated, container, false);
        tv = (TextView) rootView.findViewById(R.id.tv);
        tts = new TextToSpeech(getActivity(), TopRatedFragment.this);
        speakOut();
        return rootView;
    }

    @Override
    public void onInit(int status)
    {
        if (status == TextToSpeech.SUCCESS)
        {
            int result = tts.setLanguage(Locale.US); 
            tts.setSpeechRate(0);  
            if (result == TextToSpeech.LANG_MISSING_DATA
                    || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Log.e("TTS", "Language is not supported");
            } else { 
                speakOut();
            }
        } else {
            Log.e("TTS", "Initilization Failed");
        }
    }

    @Override
    public void onDestroy()
    { 
        if (tts != null) {
            tts.stop();
            tts.shutdown();
        }
        super.onDestroy();
    }

    private void speakOut()
    {
        tts.speak(tv.getText().toString(), TextToSpeech.QUEUE_FLUSH, null);
        Log.e("root ",  tv.getText().toString());
    }
}

The text in textview is reading out. But the text of 1st fragment is read in 2nd fragment that too only when I swipe backward.

fragment 3 --> fragment 2 (here fragment 1 text is read).

I really do not understand what is happening.

And when I open the app from recently opened apps list then it is reading its own text.

Kindly help if possible....Thank you..


Solution

  • Well the problem is that at start- Fragment1 is loaded along with Fragment2. So u can hear contents from Fragment1 and Fragment2 resp.

    Now when u navigate to Fragment2 [already loaded so you cannot hear chapter2] then Fragment3 is loaded so you can hear Chapter3.

    When you navigate to Fragment3, then Fragment1 is removed.

    Now if you navigate from Fragment3 to Fragment2 then Fragment1 is loaded. Hence u can hear chapter1.

    Solution:- Simply check if the Fragment is visible and then perform the rest of action. In your case TTS thing.

    1. Extend FragmentPagerAdapter.
    2. override setUserVisibleHint().

      @Override
      public void setUserVisibleHint(boolean isVisibleToUser)
      {
          super.setUserVisibleHint(isVisibleToUser);
          Log.d(TAG, "setUserVisibleHint: " + isVisibleToUser);
      }
      

    Now u can perform ur action inside setUserVisibleHint() if(isVisibleToUser){} instead of onCreateView() okay.