如何从专辑ID获取歌曲和其他媒体? [英] How to get songs and other media from an Album ID?

查看:136
本文介绍了如何从专辑ID获取歌曲和其他媒体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从专辑ID中获取歌曲和其他媒体详细信息.我所拥有的只是专辑ID,我尝试了许多解决方案,但没有一个成功.

I want to get songs and other MEDIA details from Album Id. All I have is Album Id and I tried many solutions but none of them succeed.

我的代码段:

ContentResolver contentResolver = getContentResolver();
        Uri mediaUri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, albumId);
        Log.wtf("SKJDBKJ", mediaUri.toString());
        Cursor mediaCursor = contentResolver.query(mediaUri, null, null, null, null);

        // if the cursor is null.
        if(mediaCursor != null && mediaCursor.moveToFirst())
        {
            Log.wtf("DSJK", "entered cursor");
            //get Columns
            int titleColumn = mediaCursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM);
            int idColumn = mediaCursor.getColumnIndex(MediaStore.Audio.Media._ID);
            int artistColumn = mediaCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST);

            // Store the title, id and artist name in Song Array list.
            do
            {
                long thisId = mediaCursor.getLong(idColumn);
                String thisTitle = mediaCursor.getString(titleColumn);
                String thisArtist = mediaCursor.getString(artistColumn);
                Log.wtf("Title", thisTitle);

                // Add the info to our array.
                songArrayList.add(new Song(thisId, thisTitle, thisArtist));
            }
            while (mediaCursor.moveToNext());

            // For best practices, close the cursor after use.
            mediaCursor.close();
        }

mediaUri的日志返回当前专辑的路径,例如:content://media/external/audio/media/41. 有人告诉我该怎么做?

Log for mediaUri returns path to current album, e.g. : content://media/external/audio/media/41. Someone tell me how do I do it?

推荐答案

经过大量的尝试和错误,我自己弄清楚了这一点.我不知道这是否是最好,最安全的方法,但是就其有效而言,我很高兴.

I have figured it out myself after a LOT of trial and errors. I don't know if it's the best and safest way to do so, but as far as it's working I am happy.

我稍微更改了代码,并比较了专辑ID.这是代码段:

I changed my code a bit and compared Album IDs. Here's the snippet:

ContentResolver contentResolver = getContentResolver();
        Uri mediaUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        Log.wtf("SKJDBKJ", mediaUri.toString());
        Cursor mediaCursor = contentResolver.query(mediaUri, null, null, null, null);

        // if the cursor is null.
        if(mediaCursor != null && mediaCursor.moveToFirst())
        {
            Log.wtf("DSJK", "entered cursor");
            //get Columns
            int titleColumn = mediaCursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
            int idColumn = mediaCursor.getColumnIndex(MediaStore.Audio.Media._ID);
            int artistColumn = mediaCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST);
            int albumId = mediaCursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID);

            // Store the title, id and artist name in Song Array list.
            do
            {
                long thisId = mediaCursor.getLong(idColumn);
                long thisalbumId = mediaCursor.getLong(albumId);
                String thisTitle = mediaCursor.getString(titleColumn);
                String thisArtist = mediaCursor.getString(artistColumn);

                // Add the info to our array.
                if(this.albumId == thisalbumId)
                {
                    Log.wtf("SAME2SAME", String.valueOf(thisalbumId));
                    Log.wtf("SAME2SAME", String.valueOf(this.albumId));
                    songArrayList.add(new Song(thisId, thisTitle, thisArtist));
                }
            }
            while (mediaCursor.moveToNext());

            // For best practices, close the cursor after use.
            mediaCursor.close();
        }

我更改了:

  1. AlbumsMediaStore.Audio.Audio.xxx中的Media.
  2. 得到Mediaalbum Id.
  3. 与我从bundle extras收到的album Id相比.
  4. 仅将这些歌曲添加到arrayList中. 我想这也是Artists的方式.
  1. Albums to Media in MediaStore.Audio.Audio.xxx.
  2. Got the album Id of Media.
  3. Compared that to album Id I receive from bundle extras.
  4. Only add those songs in the arrayList. I guess this'll be the way for Artists too.

这篇关于如何从专辑ID获取歌曲和其他媒体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆