All CategoriesAndroid App Development

Load mp3 Resource from Raw Folder in Android

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.

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.

Related Popular Courses:

ANDROID ONLINE COURSE

BIG DATA COURSE SYLLABUS

BLOCKCHAIN TECHNOLOGY APPLICATIONS

BUSINESS ANALYTICS COURSES IN INDIA

DATA SCIENTIST ONLINE COURSE

Android Programming

Tags

3 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Articles

Close
Close