Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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


Solution 2:

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

Post a Comment for "Play Mp3 Sounds From Sd Card"