android - 获取歌曲流派的最有效方法 android

标签 android mediastore

我在加载歌曲时尝试获取每首歌曲的流派。目前我正在使用这个解决方案:

MediaMetadataRetriever mr = new MediaMetadataRetriever();
Uri trackUri = ContentUris.withAppendedId(
                  android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                  cursor.getLong(id));
mr.setDataSource(c, trackUri);
String genre = mr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_GENRE);

它确实有效,但需要非常长的时间。在我的测试手机上,只有大约 20 首歌曲,速度很快,但在我的主手机上,有 1700 多首歌曲,它需要很长时间。如果我删除该代码并仅加载歌曲,它几乎是即时的,所以我知道它必须是这样的。有没有更好/更有效的方法来获取带有歌曲 ID 的歌曲流派?

预先感谢您的帮助!

完整代码 fragment

    try {
        if (cursor.moveToFirst()) {
            int id = cursor.getColumnIndex(MediaStore.Audio.Media._ID);
            int title = cursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
            int album = cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM);
            int artist = cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST);
            int duration = cursor.getColumnIndex(MediaStore.Audio.Media.DURATION);
            int albumId = cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID);
            int data = cursor.getColumnIndex(MediaStore.Audio.Media.DATA);



            do {

                MediaMetadataRetriever mr = new MediaMetadataRetriever();
                Uri trackUri = ContentUris.withAppendedId(
                        android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                        cursor.getLong(id));
                mr.setDataSource(c, trackUri);
                String genre = mr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_GENRE);

                if (genre == null){
                    genre = "Not Specified";
                }


                 genres.add(genre);

                Song item = new Song(cursor.getLong(id), cursor.getString(title),
                        cursor.getString(artist), cursor.getString(album),
                        cursor.getLong(duration),
                        ContentUris.withAppendedId(albumArtUri, cursor.getLong(albumId)),
                        Uri.parse(cursor.getString(data)), "");

                items.add(item);
            } while (cursor.moveToNext());
        }
    } finally {
        cursor.close();
    }

最佳答案

 //This Method is Use For get LIST OF GENRES_NAME or ID Or NUMBER_OF_SONG In GENRES
public void getListofGenres(Context c) {
    ArrayList<Song> songs = new ArrayList<>();

    String[] projection = new String[]{

            MediaStore.Audio.Genres.NAME,
            MediaStore.Audio.Genres._ID
    };

    Cursor cursor = c.getContentResolver().query(
            MediaStore.Audio.Genres.EXTERNAL_CONTENT_URI,
            projection,
            null,
            null,
            null);
    while (cursor.moveToNext()) {
        songs.add(addGenres(cursor));
    }
    cursor.close();


}

private Song addGenres(Cursor cursor) {
    Song song = new Song();
    //set name of Genres
    song.setAlbum(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Genres.NAME)));
    //set id of Genres
    song.setGenre(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Genres._ID)));

    Uri uri = MediaStore.Audio.Genres.Members.getContentUri("external", cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Genres._ID)));
    String[] projection = new String[]{MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media._ID};
    Cursor cur = c.getContentResolver().query(uri, projection, null, null, null);
    //set Number Of songs in Genres
    song.setSize("" + cur.getCount());


    song.setPlaying(false);
    return song;
}


//This methos is use for get availabe song in GENRES
public void getGenresdata(Context c, String genresID) {
    //Arraylist for  add song songDetails
    ArrayList<Song> songslist = new ArrayList<>();

    String[] projection = new String[]{
            MediaStore.Audio.Media.TITLE,
            MediaStore.Audio.Media.ALBUM,
            MediaStore.Audio.Media.ARTIST,
            MediaStore.Audio.AudioColumns.SIZE,
            MediaStore.Audio.AudioColumns.DURATION,
            MediaStore.Audio.AudioColumns.DATE_ADDED,
            MediaStore.Audio.Media.DATA,
            MediaStore.Audio.Media.ALBUM_ID
    };

    Uri uri = MediaStore.Audio.Genres.Members.getContentUri("external", Long.parseLong(genresID));
    Cursor cursor = c.getContentResolver().query(
            uri,
            projection,
            null,
            null,
            null);
    while (cursor.moveToNext()) {
        //get data like this and add data in model class
        Song song = new Song();
        song.setTitle(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA)));
        //Note: - We use all  MediaColumns declare in projection
        songslist.add(song);
    }
    cursor.close();

}

关于android - 获取歌曲流派的最有效方法 android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44379872/

相关文章:

使用 GoogleCloudMessaging 的 Android gcm 客户端

java - 使用 Sphinx 4 语音识别平台效果不佳

android - 进度对话框在 android 的 asynctask 中旋转得不够

java - XML 淡出动画不起作用

Android: picasso 加载图片失败。如何显示错误信息

android - 从 MediaStore 获取视频的添加/修改/拍摄日期

android - MediaStore - BUCKET_DISPLAY_NAME 仅出现在 API 29+ 上?

android - 仅在未实现 onError 时使用 RxJava 的全局错误处理解决方案

android - 从手机存储中获取和调整位图大小的最佳方法

android - 如何在Android中的 ListView 中显示存储中的doc、docx、pdf、xls、txt