Right place for putting mp3 files in an android project
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);
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