Android Adding Music to your App

A

Hello guys today I’d like to discuss how I got the music to play in the splash screen here. First off, create a MediaPlayer object.

1> MediaPlayer song = MediaPlayer.create(context, reid);

It takes in a context, in our case of Splash.this – the class is known as splash. And the “reid” is where the song is located. The song should be in the resource(res) folder. Add a new folder where you will be keeping all your sound. It should finally look like:

To start the song you use:

1> song.start();

And because this was a splash screen, the song had to end when the splash screen ends or pauses. So, at the onPause() method I added:

1> song.release();

And that will end the song.

package com.learning.gilo;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;

public class Splash extends Activity{

MediaPlayer song;

protected void onCreate(Bundle b){
super.onCreate(b);

setContentView(R.layout.splash);

song = MediaPlayer.create(Splash.this, R.raw.door); //the song is door.mp3
song.start();

Thread mythread = new Thread(){
public void run(){
try{
sleep(2000);
}catch(InterruptedException e){
e.printStackTrace();
}finally{
Intent myIntent = new Intent("com.learning.gilo.MENULIST");
startActivity(myIntent);
}
}

};

mythread.start();

}

protected void onPause(){
super.onPause();
song.release();
finish();
}
}

About the author

abhijit.kurlekar
By abhijit.kurlekar

Category