Search code examples
javaandroidsplash-screen

Play audio when splash screen start


there is a method to play a music when the splash screen is run? this is my splash screen code:

// Splash screen timer
private static int SPLASH_TIME_OUT= 2000;

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

    new Handler().postDelayed(new Runnable() {



        @Override
        public void run() {
            // This method will be executed once the timer is over
            // Start   main activity
            Intent i = new Intent(SplashScreen.this, MyActivity.class);
            startActivity(i);

            // close  activity
            finish();
        }
    }, SPLASH_TIME_OUT);
}

I created the raw folder in res / raw and put in my song, how can I play it when the run splash screen?


Solution

  • Simply you can use,

    myMediaPlayer = MediaPlayer.create(this, R.raw.loveme);
        if (myMediaPlayer != null) {
                myMediaPlayer.start();
            } else {
                myMediaPlayer.reset();
                try {
                    myMediaPlayer.prepare();
                } catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    
                myMediaPlayer.start();
            }
    
        }