1

I want to list all the available AUDIO (.mp3) files in Mobile device.

User can select any Audio file from list and can set as a Notification Tone for this App.

I worked out many sources and none of them satisfiable.

Thank you.

Shamili Rani
  • 345
  • 1
  • 4
  • 12

1 Answers1

1

First get all the available mp3 files and retreive their names using the code below do whatever u want with the retreived data for eg u can set up to a list view or showing them in a dialog etc.

  String extPath = getSecondaryStorage();
           if (extPath != null)
            { mySongs_onSystem = findSongs(new File(extPath));
         }
            else
                mySongs_onSystem = findSongs(Environment.getExternalStorageDirectory());

             public ArrayList<File> findSongs(File root) {
                    ArrayList<File> al = new ArrayList<>();
                    File[] files = root.listFiles();
                    for (File singleFile : files) {
                        if (singleFile.isDirectory()) {
                            al.addAll(findSongs(singleFile));
                        } else {
                            if (singleFile.getName().endsWith(".mp3") || singleFile.getName().endsWith(".Mp3") || singleFile.getName().endsWith(".wav")) {
                                al.add(singleFile);
                            }
                        }

                    }
                    return al;
                }
 private String getSecondaryStorage() {


        String strSDCardPath = System.getenv("SECONDARY_STORAGE");

        if ((strSDCardPath == null) || (strSDCardPath.length() == 0)) {
            strSDCardPath = System.getenv("EXTERNAL_SDCARD_STORAGE");
        }

        //If may get a full path that is not the right one, even if we don't have the SD Card there.
        //We just need the "/mnt/extSdCard/" i.e and check if it's writable
        if (strSDCardPath != null) {
            if (strSDCardPath.contains(":")) {
                strSDCardPath = strSDCardPath.substring(0, strSDCardPath.indexOf(":"));
            }
            File externalFilePath = new File(strSDCardPath);

            if (externalFilePath.exists() && externalFilePath.canWrite()) {
                return strSDCardPath;
            }
        }
        return null;
    }

retreive the names of all the mp3 files using a for loop like this.

    for (int i = 0; i < mySongs_onSystem.size(); i++) {
    //declare a string array in global String[] songNames;
 songNames[i] = mySongs_onSystem.get(i).getName().toString().replace(".mp3", "").replace(".Mp3", "").replace(".wav", "");
            Log.i("songname", songNames[i]);
        }

pass the string array of retreived mp3 tones and attach it to list adapter to show all song names in a list and attach a click listener and

store the uri of the selected mp3 
Uri u = Uri.parse(mySongs_onSystem.get(position).getAbsolutePath());

create a custom interface which gets triggered when notification arrives and then play the selected mp3 audio using media player like this:

  mMediaPlayer = new MediaPlayer();
     mMediaPlayer = MediaPlayer.create(getApplicationContext(),u);
    mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mMediaPlayer.setLooping(true);
    mMediaPlayer.start();
Avinash Roy
  • 953
  • 1
  • 8
  • 25
  • Thanks Roy for your response, its working but this piece of code getting System Ringtone (Mobile device default Ringtone not .Mp3 format files) only. I need all .mp3 format files (Eg : Movie Songs also) to be list. – Shamili Rani Jan 17 '17 at 06:55
  • first loop through the Environment.getExternalStorageDirectory() like this: ArrayList mySongs_onSystem = findSongs(Environment.getExternalStorageDirectory()); – Avinash Roy Jan 17 '17 at 07:30
  • Worked like a Charm. You saved my time. Environment.getExternalStorageDirectory() is referring to SdCard0 i.e Internal Storage, i'll check out that one. Thank you. – Shamili Rani Jan 17 '17 at 08:08
  • Do u want me to include the code for retreiving the external sd card explicitly since Environment.getExternalStorageDirectory() returns everything related to storage (i.e external and internal). Should i add the code for retreiving files explicitly from the sd card??? – Avinash Roy Jan 17 '17 at 09:08
  • Yes I tried one piece of code but that is not working well with different devices, its Behaviour varying from device to device. Can you try this issue once. – Shamili Rani Jan 18 '17 at 12:36
  • I just added a method to retreive the Secondary Storage (i.e getting the path for the external sd card and reading it),This is foolproof method of reading the external memory. i have tested it in Samsun Galaxy Grand and Moto G,It works, Hope u run this and get the output. Happy Coding!! – Avinash Roy Jan 19 '17 at 05:33
  • Just Let me know in case You get the list of the songs..!! @ShamiliRani – Avinash Roy Jan 19 '17 at 06:49