【问题标题】:Slow loading times in MusicPlayerAppMusicPlayerApp 中的加载时间很慢
【发布时间】:2021-11-10 14:20:32
【问题描述】:

嘿,所以我刚刚制作了一个 Android 应用程序,它基本上从您的手机中获取所有 mp3/wav 文件,然后将它们显示给您。然后你可以玩它们。但我的加载时间很慢。我的手机有 10 mp3 和它的 6GB RAM 手机。但是加载所有这些文件大约需要 1 分钟。我正在使用内置的 MediaPlayer 类。有什么建议吗?

【问题讨论】:

  • 请编辑问题以将其限制为具有足够详细信息的特定问题,以确定适当的答案。

标签: java android xml performance android-studio


【解决方案1】:

使用此代码。

供参考link

  public void getMp3Songs() {

        Uri allsongsuri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";

        songsList.clear();

        Cursor cursor = managedQuery(allsongsuri, null, selection, null, null);

        if (cursor != null) {
            if (cursor.moveToFirst()) {
                do {
                    String song_name = cursor
                            .getString(cursor
                                    .getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
                    int song_id = cursor.getInt(cursor
                            .getColumnIndex(MediaStore.Audio.Media._ID));

                    String fullpath = cursor.getString(cursor
                            .getColumnIndex(MediaStore.Audio.Media.DATA));
                    songsList.add(fullpath);

                    String album_name = cursor.getString(cursor
                            .getColumnIndex(MediaStore.Audio.Media.ALBUM));
                    int album_id = cursor.getInt(cursor
                            .getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));

                    String artist_name = cursor.getString(cursor
                            .getColumnIndex(MediaStore.Audio.Media.ARTIST));
                    int artist_id = cursor.getInt(cursor
                            .getColumnIndex(MediaStore.Audio.Media.ARTIST_ID));


                } while (cursor.moveToNext());

            }
            cursor.close();

        }

        Toast.makeText(context, ""+songsList.size(), Toast.LENGTH_SHORT).show();
    }

【讨论】:

  • 这样会加快加载速度吗?
  • 是的,当然这会起作用。
猜你喜欢
  • 1970-01-01
  • 2011-07-11
  • 1970-01-01
  • 1970-01-01
  • 2013-11-07
  • 2018-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多