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

℡╲_俬逩灬. 提交于 2019-12-11 04:09:52

问题


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.


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


来源:https://stackoverflow.com/questions/41613154/programatically-set-custom-notification-ringtone-choose-from-audio-files-in-mob

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!