Free Shipping

Secure Payment

easy returns

24/7 support

  • Home
  • Blog
  • Load mp3 Resource from Raw Folder in Android

Load mp3 Resource from Raw Folder in Android

 July 14  | 0 Comments

Introduction

In android, raw is included in the res folder of android project. You can add an assets files in raw folder like music files, database files or text files. It is an inbuilt folder named as “raw” where you can add your files and you can retrieve the file in your java code as R.raw.(filename).

Let’s see real time implementation. If you are creating any music based application and you want to store your music files, then you can use raw folder to store your music files. i.e add music file like music.mp3 in raw folder and access it in java as R.raw.music.

Get Skilled in Android Development

MediaPlayer class is used for accessing any media files from raw folder
Example
For example we will add the music files in ListView and after clicking it will play.
First we have to create a new folder in app/res named as a ‘raw’. In this folder, we can add the music files – Here I am adding music.mp3
To create a new folder, right click on res and go to new > Directory > Give the name as ‘raw’ and you can see the folder added in your res directory.
Copy and Paste any music files into it. (Here I have added music.mp3)
Add a ListView in activity_main.xml

<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

Initialize ListView and MediaPlayer classes and create an array for list items and resources.

//Add ListView class
ListView mainList;
//Add MediaPlayer class
MediaPlayer mp;
//Add a list items in String
final String[] listContent = {"Moh Moh Ke Dhaage", "Iktara"};
//Add a resource of music files in Array
final int[] resID = {R.raw.music, R.raw.music};

Add below code in onCreate(){..}

//add a listview and make a casting on it.
mainList = (ListView) findViewById(R.id.listView);
//Add an ArrayAdapter to assign your listview and array of list items(here listContent)
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listContent);
//Set your adapter to listView.
mainList.setAdapter(adapter);
//Click on any List Item and you can play a song/music file from the list.
//For this, Add a setOnItemClickListener(){..} into it.
mainList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
mp.reset();// stops any current playing song
mp = MediaPlayer.create(getApplicationContext(), resID[i]);
mp.start(); // starting mediaplayer
}
});

Add below method onDestroy(){..} .

//whenever your music files has been stopped or completed.
@Override
public void onDestroy() {
super.onDestroy();
mp.release();
}

Output :

Download Project Link: https://github.com/hiteshbpatel/Android_Projects/tree/master/audioplay

From the Android documentation, the raw/ directory is used for: Arbitrary files to save in their raw form. To open these resources with a raw InputStream, callResources.openRawResource() with the resource ID, which is R.raw.filename. However, if you need access to original file names and file hierarchy, you might consider saving some resources in the assets/ directory (instead of res/raw/). Files in assets/ are not given a resource ID, so you can read them only using AssetManager.

Note: The files in the raw/ directory are not compiled by the platform. They are assigned as a resource ID and cannot be grouped into sub-folders whereas if you want otherwise you can use the assets/directory.

Keep visiting our site www.acadgild.com for more updates on Android and other technologies.

>