Is there any folder like res/drawable for mp3 or generally audio files? If yes, what is it and how can I get access to it from the app?
-
1Raw resources are not compressed or manipulated in any way when packaged into your application, making them an ideal way to store precompressed files such as audio files. – Prateek Joshi Jul 09 '15 at 19:07
-
you can use either raw folder or asset folder........ for raw refer this https://stackoverflow.com/questions/7499605/how-to-play-the-audio-files-directly-from-res-raw-folder for asset refer this code https://stackoverflow.com/questions/9493983/save-audio-file-in-raw-or-assets-folder-to-sdcard-android also refer difference between Asset & Raw https://stackoverflow.com/questions/5583608/android-difference-between-res-and-assets-directory – Dheeresh Singh Jun 09 '12 at 08:39
4 Answers
The best place to put such .mp3
or any other files would be in the assets
folder.
These files once stored will become a part of your android app itself and can be read easily. This tutorial describes it well.
AssetFileDescriptor afd = getAssets().openFd("AudioFile.mp3");
MediaPlayer player = new MediaPlayer();
player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
player.prepare();
player.start();
Alternatively you can also store it in the raw
folder and read it directly by specifying the path as the raw folder.
this can be played as:
int resID=getResources().getIdentifier(fname, "raw", getPackageName());
MediaPlayer mediaPlayer=MediaPlayer.create(this,resID);

- 1
- 1

- 4,189
- 4
- 31
- 53
-
8
-
I followed the asset option with some luck. I added a flag to this code to keep from replaying the mp3 every time a play button was clicked. – Benjamin Castor May 14 '16 at 20:41
-
-
I was wondering if we can initially make a database and store the files in the database itself? – juztcode Nov 12 '19 at 13:21
-
@juztcode No, It's not recommended to put files in database (For example it will cause increasing db size and as a result will slow down loading db). – Masoud Maleki Jul 08 '21 at 06:06
-
Difference between the raw folder and the assets folder: https://stackoverflow.com/questions/9563373/when-should-i-use-the-assets-as-opposed-to-raw-resources-in-android – Masoud Maleki Jul 08 '21 at 06:06
Here are some steps you can easily follow.
Open the android studio with the project in which you want to add-on audio clip/media file.
Create a
raw
folder in the resources folder.Add media file to the
raw
folder by simply copy and paste that to theraw
folder.Here we added a media file “ring.mp3”. Now open the Java File of the desired activity, here we are adding the audio in the MainActivity.
Further add this code.
MediaPlayer ring = MediaPlayer.create(MainActivity.this, R.raw.ring);
ring.start();
Now run the App and your music will play when App starts

- 9,724
- 11
- 63
- 92

- 772
- 8
- 11
You should save the .mp3 into res/raw
. AndroidStudio recognizes the raw
folder. (By contrast, it does not automatically recognize a res/assets
folder).
To play music.mp3:
mediaPlayer = MediaPlayer.create(ctx, R.raw.cat_meow);
mediaPlayer.start();
Note the convenient use of R.
syntax.

- 998
- 1
- 11
- 15
Place it into your assets folder. Preferably under assets/raw/myfile.mp3 You can access it using:
String mp3File = "raw/music.mp3";
AssetManager assetMan = getAssets();
MediaPlayer media = new MediaPlayer();
FileInputStream mp3Stream = assetMan.openFd(mp3File).createInputStream();
media.setDataSource(mp3Stream.getFD());
media.prepare();
media.start();

- 8,555
- 3
- 34
- 44
-
This is not working. Perhaps assets/ should be removed from the path. – Mohammad Moghimi Jun 24 '12 at 18:25