android - 以编程方式为应用程序设置自定义通知铃声(从移动设备中的音频文件中选择)

标签 android notifications mp3 ringtone custom-notification

我想列出移动设备中所有可用的音频 (.mp3) 文件。

用户可以从列表中选择任何音频文件,并可以将其设置为该应用程序的通知音。

我找到了很多来源,但没有一个是可满足的。

谢谢。

最佳答案

首先获取所有可用的 mp3 文件并使用下面的代码检索它们的名称对检索到的数据做任何您想做的事情,例如您可以设置 ListView 或在对话框中显示它们等。

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

像这样使用 for 循环检索所有 mp3 文件的名称。

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

传递检索到的 mp3 音调的字符串数组并将其附加到列表适配器以在列表中显示所有歌曲名称并附加点击监听器和

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

创建一个在通知到达时触发的自定义界面,然后像这样使用媒体播放器播放选定的 mp3 音频:

  mMediaPlayer = new MediaPlayer();
     mMediaPlayer = MediaPlayer.create(getApplicationContext(),u);
    mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mMediaPlayer.setLooping(true);
    mMediaPlayer.start();

关于android - 以编程方式为应用程序设置自定义通知铃声(从移动设备中的音频文件中选择),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41613154/

相关文章:

android - 可以将 fragment 放入 RecyclerView 或 ListView 中吗?

android - 如果我提交具有不同项目顺序的相同列表,ListAdapter 不会刷新 RecyclerView

database - 用于记录用户操作的优雅模式

swift - 经过一定时间后触发 Xcode 通知 (Swift 3)

android - 为 7 英寸平板电脑创建特定布局以更改布局的纵向/横向

android - 约束布局获取 ImageView 的默认上边距

java - 使用 xuggler 从图片创建 mp4 和 java mp3

Android - 将 mp3 音频添加到 mp4 视频

android - 如何在 Android 中堆叠推送通知?

android - 设置联系人自定义铃声,怎么办?