Android Mediastore:如何高效检索某种流派的所有歌曲?

发布于 2024-12-21 07:05:17 字数 236 浏览 0 评论 0原文

我知道如何检索特定歌曲的流派(请参阅获取流派),但我想检索特定流派的所有歌曲。由于“流派”似乎不是媒体项目的列之一,因此我不知道如何在单个查询中执行此操作,这与艺术家或专辑不同。有没有有效的方法呢?谢谢!

I know how to retrieve the genre of a particular song, (see getting the genres), but I want to retrieve all songs of a particular genre. Since "genre" does not seem to be one of the columns for a media item, I don't know how to do it in a single query, unlike artist or album. Is there an efficient method? Thanx!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抱猫软卧 2024-12-28 07:05:17
Uri uri = Audio.Genres.Members.getContentUri("external", genreID); 

String[] projection = new String[]{Audio.Media.TITLE, Audio.Media._ID};

Cursor cur = contentResolver.query(uri, projection, null, null, null);
Uri uri = Audio.Genres.Members.getContentUri("external", genreID); 

String[] projection = new String[]{Audio.Media.TITLE, Audio.Media._ID};

Cursor cur = contentResolver.query(uri, projection, null, null, null);
红衣飘飘貌似仙 2024-12-28 07:05:17

您可以通过组合内容 URI 并查询 MediaStore 来完成此操作。下面是从 Android 音乐播放器借用的一些代码:

String [] cols = new String [] {MediaStore.Audio.Genres.NAME};
Cursor cursor = MusicUtils.query(this,
                ContentUris.withAppendedId(MediaStore.Audio.Genres.EXTERNAL_CONTENT_URI, Long.valueOf(mGenre)),
                cols, null, null, null);

这是 MusicUtils 中的代码:

public static Cursor query(Context context, Uri uri, String[] projection,
        String selection, String[] selectionArgs, String sortOrder, int limit) {
    try {
        ContentResolver resolver = context.getContentResolver();
        if (resolver == null) {
            return null;
        }
        if (limit > 0) {
            uri = uri.buildUpon().appendQueryParameter("limit", "" + limit).build();
        }
        return resolver.query(uri, projection, selection, selectionArgs, sortOrder);
     } catch (UnsupportedOperationException ex) {
        ErrorReporter.getInstance().putCustomData("UnsupportedOperationException", "true");
        return null;
    }

}

You can do this by putting together a content URI and querying the MediaStore. Here's some code borrowed from the Android music player:

String [] cols = new String [] {MediaStore.Audio.Genres.NAME};
Cursor cursor = MusicUtils.query(this,
                ContentUris.withAppendedId(MediaStore.Audio.Genres.EXTERNAL_CONTENT_URI, Long.valueOf(mGenre)),
                cols, null, null, null);

This is the code in MusicUtils:

public static Cursor query(Context context, Uri uri, String[] projection,
        String selection, String[] selectionArgs, String sortOrder, int limit) {
    try {
        ContentResolver resolver = context.getContentResolver();
        if (resolver == null) {
            return null;
        }
        if (limit > 0) {
            uri = uri.buildUpon().appendQueryParameter("limit", "" + limit).build();
        }
        return resolver.query(uri, projection, selection, selectionArgs, sortOrder);
     } catch (UnsupportedOperationException ex) {
        ErrorReporter.getInstance().putCustomData("UnsupportedOperationException", "true");
        return null;
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文