【问题标题】:Get list of mp3 files from assets folder and save in shared preference从 assets 文件夹中获取 mp3 文件列表并保存在共享首选项中
【发布时间】:2021-01-08 08:23:48
【问题描述】:

我有一个应用程序,其中资产文件夹中有一些 mp3 声音。在应用程序开始时,我获取文件列表并将所有文件从资产复制到 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 中,但它发生在随机设备上。有些完美地播放声音,有些则保持沉默或发出哔哔声。我缺少什么或我的代码有什么问题? 请帮忙

【问题讨论】:

  • 你告诉了应该发生的事情。但你没有告诉一切是否如你所愿。例如,mediascanner 是否为您提供每个文件的 uri?
  • 在某些 android 设备上,是的,但在某些设备上,它不会为任何文件提供 uri。这就是问题
  • 尤其是在 android 10 中,mediascanner 不提供任何 uri
  • Mediascanner 正在某些设备上工作,所以我不知道这是否是问题。
  • 你知道怎么解决吗?

标签: android audio sharedpreferences assets android-assetmanager


【解决方案1】:

媒体扫描器不会索引您应用程序私有目录中的文件。不再来自 getExternalFilesDir()。

但您不需要媒体扫描仪来获取 uri 或将信息保存在共享首选项中。

只需使用 FileProvider 来提供您的文件。

另一种方法是将文件直接复制到媒体存储。然后,您将拥有不错的媒体存储库,并且您的文件也会被扫描。

第三种可能性是使用您自己的 ContentProvider 并直接从资产中提供您的文件。无需先复制。

【讨论】:

  • 谢谢,让我试试这个:)
  • 您能否分享第三个选项的任何示例或您编写的任何选项?我是文件提供者和内容提供者的新手。任何帮助链接都会很有用。谢谢你
  • 谢谢。我会试试这个
  • 我认为你最好看看这里,因为第一个链接据我所知没有源代码。阅读所有的 cmets。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多