Right place for putting mp3 files in an android project - android

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?

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 the raw 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

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.

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();

Related

How to point to sound files that are in assetpack\src\main\assets and not in app\src\main\res\raw

I completed my first app and could not publish it because the file size was to big (lots of sound files). I followed the Play Asset Delivery (https://developer.android.com/guide/app-bundle/asset-delivery/build-native-java) and it looks I got everything configured correctly (it builds and app loads but no sounds) but need help pointing to the sound files in the code. The sound files were in res>raw are now in assetpack\src\main\assets
When the sound files were in res>raw. I used this code to point to the sound file
if (sound == 1) { mp = MediaPlayer.create(this#MainActivity, R.raw.sound_1) }
Now I cannot point to the sound files in assets and I think its because it does not see it or knows that the folder exist.
When I change the left panel view to Project I see the list below and assetpack is there
Project
App name
.gradle
.idea
app
assetpack
build
gradle
play-core-native-sdk
other files
When I change the left panel view to Android I see the below list and assetpack is not part of it
Android
app
manifests
java
java (generated)
cpp
res
raw
I don't understand how this works and tried searching here and google with no help
Can you help me point to assetpack\src\main\assets sound files?
Thanks!
You can use the following code to play a specific audio file (i.e your_file.mp3) in your assets folder with:
MediaPlayer mediaPlayer = new MediaPlayer();
AssetFileDescriptor afd = context.getAssets().openFd("your_file.mp3");
mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
afd.close();
mediaPlayer.prepare();
mediaPlayer.start();

In Android Studio Version 3.0, I am unable to find raw folder inside the res folder

I have already created the folder inside the res directory. Out Android studio, I can easily access the raw folder but Inside the android studio, It is not displaying and I am unable to get access the files inside it.
And If I select the folder structure to project it is appearing.
Its, raw instead of Raw!
We can use following line to access elements from raw folder.
getResources().openRawResource(resourceName);
If you are looking for read text file from raw assets then you can use following sample of code.
InputStream raw = context.getAssets().open("filename.txt");
Reader is = new BufferedReader(new InputStreamReader(raw, "UTF8"));
Tho I had a folder named "raw" I do have a similar issue and after a couple of restarting android studio Worked fine for me, Or else you can try this
Uri myUri = ....; // initialize Uri here
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(getApplicationContext(), myUri);
mediaPlayer.prepare();
mediaPlayer.start();

Handle audio file on Android

I have an audio file on drawable folder.
Suppose that the filename is: "abc.mp3".
I would use this code to reproduce the sound:
MediaPlayer mPlayer = MediaPlayer.create(this, Uri.parse("pathFile"));
What's the path of my file?
Thank you!
If you don't want to place the audio file in the assets folder, you can place it in the "raw" folder inside the resources folder. Thus, /res/raw. Then access it like this:
MediaPlayer mPlayer = MediaPlayer.create(this, R.raw.abc);
// enable loop and play
mPlayer.setLooping(true);
mPlayer.start();
You should be placing audio file in the assets folder, not in the drawable folder.
After placing your file in the assets folder you can follow this SO post : Play audio file from assets directory.

Playing sound files from expansion file

I have an expansion file with a series of sound files. The longer ones are .mp3 files while the shorter sound effects are .wav files. I pack them in a .zip file with no compression. I use the Zip File reader and downloader library provided by Google.
I'm opening the sound files like so:
MediaPlayer mPlayer;
...
AssetFileDescript asd = expansionFile.getAssFileDescriptor(fileName);
FileDescriptor soundFile = asd.getFileDescriptor();
mPlayer = new MediaPlayer();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer.setDataSource(soundFile);
mPlayer.prepare();
mPlayer.start();
The odd thing is that it plays the same sound file every single time. It seems to be whatever it finds first. The byte size for asd.getDeclaredLength() and asd.getLength() is exactly what it should be. This means the file descriptor is at least finding the file, but for some reason the MediaPlayer plays whatever random file it decides.
What exactly am I missing here? Any help would be greatly appreciated.
Thanks.
The first approximation is correct, except for when the zip file contains more than one file, then you should use:
mPlayer.setDataSource(asd.getFileDescriptor(), asd.getStartOffset(), asd.getLength());
You can get more information from this other question:
Play audio file from the assets directory
Please, also remember to close the AssetFileDescriptor just after setDataSource to media player:
asd.close();
http://developer.android.com/reference/android/media/MediaPlayer.html#setDataSource%28java.io.FileDescriptor,%20long,%20long%29

How do I play multiple songs stored in my raw folder using MediaPlayer in android?

How do I play multiple songs that I have stored in a raw folder using MediaPlayer using seek and previous? Thanks.
To play a file from your Android project's raw folder:
MediaPlayer mediaPlayer = MediaPlayer.create(yourActivity, R.raw.filename);
mediaPlayer.setLooping(false);
mediaPlayer.start();
mediaPlayer.release();
If you have multiple files and want to use next/previous, just create an ArrayList< Object > with your files and write something to select the files from that list.

Categories

Resources