Handle audio file on Android - 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.

Related

How to initialize MediaPlayer in android without an R.raw file

How do I initialize MediaPlayer in Android without an R.raw or some kind of file?
If I don`t initialize MediaPlayer with some file I will get a null pointer exception at runtime. But when the program is started there is no file path to use because the user has not yet pressed a button to select the mp3 file from the SD card.
When initialzing the Media Player there is another probem. It only takes an R.raw type file, not the path of a file from the SD card. And if I don't have any file in the R.raw directory then I can`t initialize with a file. It appears that you need an audio file in the local R.raw folder to do this.
Mediaplayer player = MediaPlayer.create(this, R.raw.sample_music);
There is a method called "setDataSource" that allows me to set the path of the file, however I have to initialize the MediaPlayer first.
player.setDataSource(selectedAudioPath);
Any other way to initialize MediaPlayer?
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(filepath);
This post helped me and the code below works as well:
Mediaplayer player = MediaPlayer.create(this, Uri.parse(File.getAbsolutePath()));

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

Right place for putting mp3 files in an android project

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

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.

Using java with android how can I get the path of my resources files?

I added some audio files to the res file.
first I created a raw dir and then I added the folder with the audio files.
How can I get each file path? (I want to use the path to play this audio files)
I found that I need to use
classLoader.getResource
but I don't understand how. what is the argument for this function?
A reference should be added to your R class that you can get as something like R.raw.sound_file_1. Check out the Audio and Video section of the Android Developer's Guide for more details.
Once you have the reference to your audio clip you can play it by:
MediaPlayer mp = MediaPlayer.create(context, R.raw.sound_file_1);
mp.start();
So you don't really need the path to the resource at all, just use the reference.

Categories

Resources