Search code examples
androidandroid-viewpagertext-to-speech

Text-to-Speech in on View Pager


  • I have view pager, and few text in every page, i have a button ,on button press text-to-speech event is fire

  • I know how to use text-to-speech, but when it comes to viewPager i dont know

  • Code :

    public class MainActivity extends Activity implements TextToSpeech.OnInitListener {

    private TextToSpeech tts;
    private Button btnSpeak;
    private EditText txtText;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
    
        tts = new TextToSpeech(this, this);
    
        btnSpeak = (Button) findViewById(R.id.btnSpeak);
    
        txtText = (EditText) findViewById(R.id.txtText);
    
        // button on click event
        btnSpeak.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View arg0) {
                speakOut();
            }
    
        });
    }
    
    @Override
    public void onDestroy() {
        // Don't forget to shutdown!
        if (tts != null) {
            tts.stop();
            tts.shutdown();
        }
        super.onDestroy();
    }
    
    @Override
    public void onInit(int status) {
        // TODO Auto-generated method stub
    
        if (status == TextToSpeech.SUCCESS) {
    
            int result = tts.setLanguage(Locale.US);
    
            // tts.setPitch(5); // set pitch level
    
            // tts.setSpeechRate(2); // set speech speed rate
    
            if (result == TextToSpeech.LANG_MISSING_DATA
                    || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Log.e("TTS", "Language is not supported");
            } else {
                btnSpeak.setEnabled(true);
                speakOut();
            }
    
        } else {
            Log.e("TTS", "Initilization Failed");
        }
    
    }
    
    private void speakOut() {
    
        String text = txtText.getText().toString();
    
        tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
    }
    

    }

-- How to implement this in viewpager,and speak(convert) text that are particular in page


Solution

  • The easiest way would be to make the tts object static in MainActivity

    Then from your fragments you can call MainActivity.tts.stop() etc.

    It would probably be better to include some helper methods in MainActivity such as:

    public static TextToSpeech getTTS(){
    return tts;
    }
    

    and to avoid null pointers:

    public static boolean isTTSAvailable(){
    return tts != null // && ttsInitialised
    }
    

    Where you would set a static boolean to true if onInit returned SUCCESS.

    MainActivity would continue to handle releasing the tts object in onDestroy etc.

    As a side note, it's always good practice to check out the memory profiler in Android Studio to make sure that holding a static reference to any object such as this has no adverse effects. Do before and after analysis and run the garbage collector manually etc to check you are holding no unwanted references after your Activity has been destroyed. This is very generic advice, but whenever making a code change such as this, it's good to know that there are no side-effects.