Welcome back friends after a long time. Apologies for the delay, this tute is going to tell you as how you can play music in your android app. Well to begin with I created a new project named as "Chooseme". What I am going to do is that when I would run my application then a music will be played and that too of my own choice. :-)
Ok friends just follow the following steps which are supported by the screenshot. First of all you should know that the activity in which I will be inserting the music is named as "Home.java" which will be the home-screen or you can call the menu screen of my application.
Follow the following steps
1. Create a sub folder named "raw" in your "res" folder.
2. Right click on "res" then "New" and then "Folder"
3. Now copy any "mp3" music in your raw folder.
4. Write the following code below the line "setContentView(R.layout.home);" of your activity. Refer the screenshot
"MediaPlayer startingmusic = MediaPlayer.create(Home.this, R.raw.musichome);
startingmusic.start();"
Ok friends just follow the following steps which are supported by the screenshot. First of all you should know that the activity in which I will be inserting the music is named as "Home.java" which will be the home-screen or you can call the menu screen of my application.
Follow the following steps
1. Create a sub folder named "raw" in your "res" folder.
2. Right click on "res" then "New" and then "Folder"
3. Now copy any "mp3" music in your raw folder.
4. Write the following code below the line "setContentView(R.layout.home);" of your activity. Refer the screenshot
"MediaPlayer startingmusic = MediaPlayer.create(Home.this, R.raw.musichome);
startingmusic.start();"
Save the project and run your app. Yes thats it!! Sweet and simple
EXPLANATION
MediaPlayer - is a class which is used when handling music related files.
startingmusic - It is just a variable name you can name it whatever you want
musichome - It is the name of my mp3 file which I pasted in raw folder
Rest all is just compulsory code like Home.this implies that we are dealing with the current class.
start() - This method plays the music and similarly you can use stop() method to stop the music.
In case you face any problem comment here.




This comment has been removed by the author.
ReplyDeleteSuperb Yaar :)
ReplyDeletethankyou!!!
ReplyDelete