android - 从 Assets 文件夹获取mp3文件列表并保存为共享首选项

标签 android audio sharedpreferences assets android-assetmanager

我有一个应用程序,其中素材资源文件夹中有一些mp3声音。在应用程序启动时,我将获取文件列表并将所有文件从 Assets 复制到getExternalFilesDir()。为每个文件调用媒体扫描器,并且仅当媒体扫描器返回一个URI时,然后在共享首选项中写入内容

 private void copyAssets() {
    AssetManager assetManager = getAssets();
    String[] files = null;
    try {
        files = assetManager.list("");
    } catch (IOException e) {
        Log.e("tag", "Failed to get asset file list.", e);
    }
    if (files != null) for (String filename : files) {
        InputStream in = null;
        OutputStream out = null;
        try {
            in = assetManager.open(filename);
            File outFile = new File(getExternalFilesDir(null), filename);
            out = new FileOutputStream(outFile);
            copyFile(in, out);

            MediaScannerConnection.scanFile(this,
                    new String[] { outFile.getAbsolutePath() }, null,
                    new MediaScannerConnection.OnScanCompletedListener() {
                        public void onScanCompleted(String contentPath, Uri uri) {
                            Log.i("onScanCompleted", uri.toString());
                            RingtoneSoundChooserFragment.path = uri.toString();
                            SharedPreferences sharedPreferences = getSharedPreferences("songs_uri",Context.MODE_PRIVATE);
                            SharedPreferences.Editor editor = sharedPreferences.edit();
                            editor.putString(filename,uri.toString());
                            editor.apply();
                        }
                    });


        } catch(IOException e) {
            Log.e("tag", "Failed to copy asset file: " + filename, e);
        }
        finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    // NOOP
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    // NOOP
                }
            }
        }
    }
}
在要加载这些声音并播放的片段中,我在名为“RingtoneSoundChooserFragment”的片段类中编写了以下代码
  List<SoundData> sounds = new ArrayList<>();
    SharedPreferences sharedPreferences = getContext().getSharedPreferences("songs_uri",Context.MODE_PRIVATE);


    sounds.add(new SoundData("Shukran", SoundData.TYPE_RINGTONE,sharedPreferences.getString("shukran.mp3","null")));
    sounds.add(new SoundData("My Shortcomings", SoundData.TYPE_RINGTONE,sharedPreferences.getString("my_shortcomings.mp3","null")));
    sounds.add(new SoundData("Need the Love", SoundData.TYPE_RINGTONE,sharedPreferences.getString("need_the_love.mp3","null")));
    sounds.add(new SoundData("La", SoundData.TYPE_RINGTONE,sharedPreferences.getString("la_ilaha_illallah.mp3","null")));
    sounds.add(new SoundData("ma", SoundData.TYPE_RINGTONE,sharedPreferences.getString("subhanallah.mp3","null")));
    sounds.add(new SoundData("Qal", SoundData.TYPE_RINGTONE,sharedPreferences.getString("baydh.mp3","null")));
    sounds.add(new SoundData("Be Yourself", SoundData.TYPE_RINGTONE,sharedPreferences.getString("be_yourself.mp3","null")));
    sounds.add(new SoundData("Jamal Ul-Wujoodi", SoundData.TYPE_RINGTONE,sharedPreferences.getString("jamal_ul_wujoodi.mp3","null")));
    sounds.add(new SoundData("Yan", SoundData.TYPE_RINGTONE,sharedPreferences.getString("ya.mp3","null")));
    sounds.add(new SoundData("Kun", SoundData.TYPE_RINGTONE,sharedPreferences.getString("kun.mp3","null")));
    sounds.add(new SoundData("Day", SoundData.TYPE_RINGTONE,sharedPreferences.getString("eid.mp3","null")));
    sounds.add(new SoundData("Sharab", SoundData.TYPE_RINGTONE,sharedPreferences.getString("sharab.mp3","null")));
    sounds.add(new SoundData("Ao", SoundData.TYPE_RINGTONE,sharedPreferences.getString("thirteen.mp3","null")));
    sounds.add(new SoundData("Hoyoo", SoundData.TYPE_RINGTONE,sharedPreferences.getString("hoyoo.mp3","null")));
    sounds.add(new SoundData("Yapo", SoundData.TYPE_RINGTONE,sharedPreferences.getString("fifteen.mp3","null")));
    sounds.add(new SoundData("mi", SoundData.TYPE_RINGTONE,sharedPreferences.getString("sixteen.mp3","null")));
    sounds.add(new SoundData("io", SoundData.TYPE_RINGTONE,sharedPreferences.getString("seventeen.mp3","null")));

    SoundsAdapter adapter = new SoundsAdapter(getAlarmio(), sounds);
    adapter.setListener(this);
    recyclerView.setAdapter(adapter);
我的适配器屏幕带有播放按钮,当我想播放声音时,请点击播放按钮。 但问题是,在某些android设备中,声音显示的是提示音,而不是实际的声音。但是,在某些设备中,它保持完全静音,而在某些设备中,它可以完美播放声音。
这个问题也出现在android 10中,但它发生在随机设备上。有些可以完美地播放声音,有些则保持沉默或发出蜂鸣声。我缺少什么或代码有什么问题? 请帮助

最佳答案

媒体扫描器不会为您的应用程序专用目录中的文件编制索引。不再来自getExternalFilesDir()。
但是您不需要媒体扫描仪来获取uri或将信息保存在共享首选项中。
只需使用FileProvider来提供文件。
一种替代方法是直接将文件复制到媒体存储。然后,您将拥有不错的媒体存储库,并且文件也将被扫描。
第三种可能性是采用您自己的ContentProvider并直接从 Assets 中提供文件。无需先复制。

关于android - 从 Assets 文件夹获取mp3文件列表并保存为共享首选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64005847/

相关文章:

audio - 如何使用 OpenNI 访问 Kinect 音频数据?

java - Android Studio 中动态列出按钮(相对布局)

Android:如何制作自定义 PreferenceScreen?

android - 将 adb 选项/参数传递给 android gradle 任务(安装、connectedCheck 等)

javascript - 如何检查音频是否未加载?

android - 在 Kotlin 中初始化 SharedPreference 的正确方法

java - 使用和/或提取可扩展文件 (obb) android

android - 如何从以特定时间间隔分隔的 N 个连接的音频样本生成音频文件?

android - 如何通过switchpreferences打开和关闭背景音乐?

android - 无法在 Android 中使用 SharedPreferences 增加计数器