RSS

Android Adding Music to your App

01 Jul

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.

 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:

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

To start the song you use:

 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:

 song.release();

And that will end the song. Simple as that. This probably doesn’t make any sense so hope over to the full source code and hopefully you will be enlightened more. There are more resources at: MediaPlayer | Android Devolopers.

Also I do plan on creating a music player that shall make use of all the MediaPlayer methods. I thought it will be cool.

Advertisements
 
4 Comments

Posted by on July 1, 2012 in Android, Code

 

4 responses to “Android Adding Music to your App

  1. Jalpa

    September 30, 2013 at 5:35 am

    but what to do if i want to continue the music during whole app

     
    • Gilbert Kimutai

      October 10, 2013 at 6:09 am

      Then you use a service. A service is different from an activity in that it doesnt have a layout. The service can contain an Array of songs (playlist) that it will play. The operations like play, pause and stop will be on an activity. The idea is you connect the activity to the service and then change or stop songs. This is the rough idea.

       

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: