1

I created a splash screen in Android Studio. Now I want a text to speech function to say:

Done by Me

This should happen when the splash screen opens. How do I go about this?

Here is my program so far:

public class CinemaList extends Activity  {
   private static int SPLASH_TIME_OUT = 4000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent homeIntent = new Intent(CinemaList.this, MovieList.class);
                startActivity(homeIntent);
                finish();
            }
        }, SPLASH_TIME_OUT);
    }
}
Elletlar
  • 3,136
  • 7
  • 32
  • 38

1 Answers1

0

I hope that you're enjoying learning Android development. It can be done as follows:

public class SplashActivity extends AppCompatActivity implements TextToSpeech.OnInitListener  {
    private TextToSpeech mTts;
    private static final String TAG = MainActivity.class.getName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTts = new TextToSpeech(this, this);
    }

    @Override
    protected void onPause() {
        if(mTts != null){
            mTts.stop();
            mTts.shutdown();
        }
        super.onPause();
    }

    @Override
    public void onInit(int status) {
        if (status == TextToSpeech.SUCCESS) {
            mTts.setOnUtteranceProgressListener(new UtteranceProgressListener() {
                @Override
                public void onDone(String utteranceId) {
                    Log.d(TAG, "Done:  " + utteranceId);
                    Intent homeIntent = new Intent(CinemaList.this, MovieList.class);
                    startActivity(homeIntent);
                }
 
                @Override
                public void onError(String utteranceId) {
                    Log.e(TAG, "Error:  " + utteranceId);
                }

                @Override
                public void onStart(String utteranceId) {
                    Log.i(TAG, "Started:  " + utteranceId);
                }
            });
            mTts.speak("Done by ME!", TextToSpeech.QUEUE_ADD, null);
        } else {
            Log.e(TAG, "Failed");
        }
    }
}

I took the liberty of starting the 'Cinema List' activity after the speech completes. However, you can continue to use your Handler if you wish.

Also, rather than calling finish() explicitly, I'd advise adding the noHistory flag to the SplashActivity in the manifest:

<activity
    android:name=".SplashActivity"
    android:noHistory="true"/>

The noHistory flag ensures that the Activity will not be there when back is pressed.

noHistory vs finish() - Which is preferred?

Elletlar
  • 3,136
  • 7
  • 32
  • 38