Shamili Rani
Shamili Rani

Reputation: 365

Programatically Set Custom Notification Ringtone (Choose from AUDIO files in Mobile) for App

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.

Upvotes: 1

Views: 1691

Answers (1)

Avinash Roy
Avinash Roy

Reputation: 973

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

Upvotes: 1

Related Questions