Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play mp3 sounds from SD card

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.

like image 328
Metallicraft Avatar asked Nov 05 '11 19:11

Metallicraft


People also ask

Can you play music on an SD card?

Users who have an Android device with an SD card slot will now be able free up their device's internal storage by saving offline music files onto an external card.

How do I get my SD card to play on my speakers?

Simply insert a micro SD card into its port at the bottom of the SP850. The speaker will need about 30-40 seconds to read the micro SD card before it can begin playing music. The order of the songs will be whatever you have set it as.

Can you put a SD card in a MP3 player?

Type of Card All of these memory cards are different and will be suitable for different MP3 players. Most media players will use either SD or micro SD cards. Some may also use mini SD cards. It's essential that you choose the right type because these are not all compatible with one another.

Can Arduino play MP3 files?

Put your SD card into the MP3 player module and If you power the Arduino UNO module, the MP3 player module will start to Play the song.


1 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

like image 69
Edu Zamora Avatar answered Nov 17 '22 15:11

Edu Zamora