Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Right place for putting mp3 files in an android project

Tags:

android

mp3

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?

like image 375
Mohammad Moghimi Avatar asked Jun 09 '12 08:06

Mohammad Moghimi


People also ask

How do I add music to my Android project?

Open your Android Studio and create a new project. Create a new folder named "raw" in your Android project's "res" folder and place your audio file inside the "raw" folder. You can see the audio is now successfully added into your Android Studio project by viewing the "raw" subfolder under "res" folder.

Where can I upload my MP3 file?

Via the providers Fidbak. audio,and Soundcloud you can upload a limited number of files for free and listen to them via a web player. All Companies offer the service to download the files or send them to other people via a private link. Wetransfer, Google Drive, PCloud, Megacloud and Dropbox are other popular options.


2 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); 
like image 82
Anurag Ramdasan Avatar answered Oct 05 '22 08:10

Anurag Ramdasan


Here are some steps you can easily follow.

  1. Open the android studio with the project in which you want to add-on audio clip/media file.

  2. Create a raw folder in the resources folder.

  3. Add media file to the raw folder by simply copy and paste that to the raw folder.

  4. 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.

  5. Further add this code.

    MediaPlayer ring = MediaPlayer.create(MainActivity.this, R.raw.ring); ring.start();

  6. Now run the App and your music will play when App starts

like image 43
Tabish khan Avatar answered Oct 05 '22 09:10

Tabish khan