Home » Android » android – Play mp3 sounds from SD card

android – Play mp3 sounds from SD card

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have a directory on my tablet’s SD card called “/Android/data/com.example.android.app/files”. I just created it manually because I don’t know how else to test this aspect of my app. I have filenames stored in a sqlite database like “/folder1/audio1.mp3”, “/folder2/audio1.mp3”, etc… The files have the same names, but are in different folders, as they are the same thing is different languages. I have those folders (folder1 and folder2) and all of the mp3 files in the “/Android/data/com.example.android.app/files” directory on the SD card.

So, I get the file names from the database and I TRY to get to the folder on the SD card with:

String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/com.example.android.app/files";

Which just basically ends up making the path “storage/Android/data/com.example.android.app/files”.

Now, I DID have a few mp3 files in my raw folder during early development, but there will be too many files in the end to keep in that folder, so I started investigating getting them from the SD card. When I had them in raw, I got them like this:

rid = getResources().getIdentifier(filname,  "raw", "com.example.android.app");

Now, with the files on the SD card, that changes to:

filename = "/folder1/audio.mp3"

and instead of “raw” I have:

"storage/Android/data/com.example.android.app/files"

and basically played them like this:

AssetFileDescriptor afd = mContext.getResources().openRawResourceFd(rid);
mMP.reset();
mMP.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength());
mMP.prepare();
mMP.start();
afd.close();

However none of this approach seems to work the same way now.

Can you get the resourceid from an mp3 file on the SD card? Is that even the appropriate way for me to do it now? Is there a different way I should be playing them that will work whether the source is internal or external memory?

Thoroughly confused.

EDIT:
I did find this which might help…but it’s maybe only have of the equation.

EDIT2:
And I’m looking here. It uses mediastore which I don’t think is what I want.

How to&Answers:

You cannot get the resource ID for an mp3 in your SD card, because this mp3 does not belong to the resources folder in your project and therefore has no resource ID.

From the Android Developers Guide:

When your application is compiled, aapt generates the R class, which contains resource IDs for all the resources in your res/ directory.

In order to be able to play mp3 from both the internal memory and the SD card, you can do something like this:

try {
    FileDescriptor fd = null;

    if (isInInternalMemory(audioFilename)) {
        int audioResourceId = mContext.getResources().getIdentifier(audioFilename, "raw", "com.ampirik.audio");
        AssetFileDescriptor afd = mContext.getResources().openRawResourceFd(audioResourceId);
        fd = afd.getFileDescriptor();
    } else if (isInSdCard(audioFilename)) {
        File baseDir = Environment.getExternalStorageDirectory();
        String audioPath = baseDir.getAbsolutePath() + audioFilename + ".mp3";
        FileInputStream fis = new FileInputStream(audioPath);
        fd = fis.getFD();
    }

    if (fd != null) {
        MediaPlayer mediaPlayer = new MediaPlayer();
        mediaPlayer.setDataSource(fd);
        mediaPlayer.prepare();
        mediaPlayer.start();
    }
} catch (Exception e) {
    e.printStackTrace();
}

This is only an example to show how it could work, obviously you should tweak it to point to the folder where your audio file is stored and handle in a proper way the exceptions you need.

EDIT:
Also, you can see a more complex example of how to play audios from the SD card here:
https://github.com/ankidroid/Anki-Android/blob/develop/AnkiDroid/src/main/java/com/ichi2/libanki/Sound.java

Answer:

root folder path may be variable in android devices, for example, it can be /mnt/sdcard in a device and another can be /sdcard/ .therefore you can use an easy way for extracting root folder by using android.os.Environment.getExternalStorageDirectory(); and after definig it, u can directory point to your directory in the internal or external path that u want.
for example: ( try catch can generated by IDE automatically)

        mediaPlayer = new MediaPlayer();
        final File root = android.os.Environment.getExternalStorageDirectory();
        try {

            mediaPlayer.setDataSource(root.getAbsolutePath() + AUDIO_FILES_DIRECTORY+"/musicFile.MP3");

        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {

            mediaPlayer.prepare();

        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }