Quantcast
Channel: CodesMeshCodesMesh
Viewing all articles
Browse latest Browse all 21

Adding Song to your Android App

$
0
0

We started from installation and configuration of android environment, then we configured emulator and played with emulator through different command from cmd. Then we create basic app on android to have sound knowledge about android app development. Now in this article you will learn how to add music mp3 file into android application. For this we have to make a new project named as songs.

songs

 

Click next and go to “configure Launcher Icon”, Here you have different different option . You can change foreground and background image of your app icon . You can also change color and shape of an image. launcher icon

At the end give the name to your activity, by default it is named as “Main Activity”.

activity

Now you will see different option here. The most important are src and res . In src you will see .java file , you can develop different application and import different library in it.

src

 

In res , you have with multiple options including images, layout of you main file , values folder with strings and style of your app.

res

 

The purpose to tell you about res is that , all images or mp3 files or all other files that you want to include in your project should include in res folder. So if we want to add mp3 file in your project you have to make a folder in res folder and add mp3 file in it. I have made a folder “raw” and drag “naat.mp3″ in it.

raw

 

Its a time to add your “naat.mp3 ” file into .java file so from src folder open “Main Activity.java”. By default your .java files looks like as follow:

javaFile

To add .mp3 file you have to create an instance of Media Player . But first create media player as mediaPlayer.create(this,resid); where resid is resource id (specify complete path where you have your .mp3 file) .

media

 

Once you create instance of media player in my case “play_song” ,you have multiple option  like you can stop, compare and start the .mp3 music.

playsong

 

Here is a complete program that tell you how to add .mp3 file in .java file

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		MediaPlayer playSong=MediaPlayer.create(this,R.raw.naat);
		playSong.start();
	}

Finally save your project and run your app as “run as android app” and enjoy your favorite song on your own app .


Viewing all articles
Browse latest Browse all 21

Trending Articles