我在加载歌曲时尝试获取每首歌曲的流派。目前我正在使用这个解决方案:
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/