获得一首歌的流派

时间:2010-09-16 16:57:00

标签: android

我如何阅读与歌曲相关的流派?我可以阅读这首歌,但是我如何抓住那首歌的流派,它存放在哪里?

谢谢!

3 个答案:

答案 0 :(得分:6)

检查此代码:

public class MusicLibraryScanner {

    private static Cursor mediaCursor;
    private static Cursor genresCursor;

    private static String[] mediaProjection = {
            MediaStore.Audio.Media._ID,
            MediaStore.Audio.Media.ARTIST,
            MediaStore.Audio.Media.ALBUM,
            MediaStore.Audio.Media.TITLE
    };
    private static String[] genresProjection = {
            MediaStore.Audio.Genres.NAME,
            MediaStore.Audio.Genres._ID
    };

    public static void getMusicFromStorage(Context context) {

        mediaCursor = context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                mediaProjection, null, null, null);

        int artist_column_index = mediaCursor
                .getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST);
        int album_column_index = mediaCursor
                .getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM);
        int title_column_index = mediaCursor
                .getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);
        int id_column_index = mediaCursor
                .getColumnIndexOrThrow(MediaStore.Audio.Media._ID);

        if (mediaCursor.moveToFirst()) {
            do {
                String info = "Song " + mediaCursor.getString(title_column_index) + " ";
                info += "from album " + mediaCursor.getString(album_column_index) + " ";
                info += "by " + mediaCursor.getString(artist_column_index) + ". ";

                int musicId = Integer.parseInt(mediaCursor.getString(id_column_index));

                Uri uri = MediaStore.Audio.Genres.getContentUriForAudioId("external", musicId);
                genresCursor = context.getContentResolver().query(uri,
                        genresProjection, null, null, null);
                int genre_column_index = genresCursor.getColumnIndexOrThrow(MediaStore.Audio.Genres.NAME);                

                if (genresCursor.moveToFirst()) {
                    info += "Genres: ";
                    do {
                        info += genresCursor.getString(genre_column_index) + " ";
                    } while (genresCursor.moveToNext());
                }

                Log.e("Audio scanner", "Song info: " + info);
            } while (mediaCursor.moveToNext());
        }
    }
}

答案 1 :(得分:0)

如果你还在寻找答案-----

MediaMetadataRetriever mr = new MediaMetadataRetriever();

Uri trackUri = ContentUris.withAppendedId(
    android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,song_id);

mr.setDataSource(mContext, trackUri);

String thisGenre = mr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_GENRE);

答案 2 :(得分:-2)

你可以从它的ID3-Tags中分别从它自己的mp3文件中获取它。应该有一些java库可以帮助你从mp3文件中访问这些标签。