如何从相册ID中获取歌曲和其他媒体?

时间:2015-09-01 17:49:26

标签: android android-mediaplayer

我想从相册ID中获取歌曲和其他媒体详情。我所拥有的只是Album Id,我尝试了很多解决方案,但没有一个成功。

我的代码段:

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。 有人告诉我该怎么做?

5 个答案:

答案 0 :(得分:4)

我自己完成了。你可以编写简单的sqlite查询来选择' contentResolver.query上的字符串。此示例可以向您展示如何按专辑ID获取歌曲。我认为这是最好的方式。

    ArrayList<Song> songs = new ArrayList<>();
    String selection = "is_music != 0";

    if (albumId > 0) {
        selection = selection + " and album_id = " + albumId;
    }

    String[] projection = {
            MediaStore.Audio.Media.TITLE,
            MediaStore.Audio.Media.ARTIST,
            MediaStore.Audio.Media.DATA,
            MediaStore.Audio.Media.DISPLAY_NAME,
            MediaStore.Audio.Media.DURATION,
            MediaStore.Audio.Media.ALBUM,
            MediaStore.Audio.Media.ALBUM_ID
    };
    final String sortOrder = MediaStore.Audio.AudioColumns.TITLE + " COLLATE LOCALIZED ASC";

    Cursor cursor = null;
    try {
        Uri uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        cursor = getActivity().getContentResolver().query(uri, projection, selection, null, sortOrder);
        if (cursor != null) {
            cursor.moveToFirst();
            int position = 1;
            while (!cursor.isAfterLast()) {
                Song song = new Song();
                song.setTitle(cursor.getString(0));
                song.setDuration(cursor.getLong(4));
                song.setArtist(cursor.getString(1));
                song.setPath(cursor.getString(2));
                song.setPosition(position);
                song.setAlbumId(cursor.getLong(6));

                cursor.moveToNext();
            }
        }

    } catch (Exception e) {
        Log.e("Media", e.toString());
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }

答案 1 :(得分:1)

经过大量的试验和错误,我已经弄明白了。我不知道这是否是最好和最安全的方式,但就其工作而言,我很高兴。

我稍微更改了我的代码并比较了相册ID。这是片段:

int zoom = (int)mMap.getCameraPosition().zoom;
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lng), zoom), 4000, null); 

我改变了:

    {li> 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(); } Albums中的Media
  1. 获得MediaStore.Audio.Audio.xxx的{​​{1}}。
  2. 将我与album Id收到的Media进行比较。
  3. 仅在album Id中添加这些歌曲。 我想这也是bundle extras的方式。

答案 2 :(得分:0)

 public Bitmap getAlbumArt(Long albumId) {
        Bitmap albumArt = null;
        try {
            final Uri AlbumArtUri = Uri.parse("content://media/external/audio/albumart");
            Uri uri = ContentUris.withAppendedId(AlbumArtUri, albumId);
            ParcelFileDescriptor pfd = GlobalSongList.getInstance().getContentResolver().openFileDescriptor(uri, "r");

            if (pfd != null) {
                FileDescriptor fd = pfd.getFileDescriptor();
                albumArt = BitmapFactory.decodeFileDescriptor(fd);
            }
        } catch (Exception e) {
        }
        return albumArt;
    }

ALBUM_ID是MediaStore中的一个字段,您可以按照查询标题,艺术家等的方式查询它。

    public Long getAlbumId(String id)
    {
        Cursor musicCursor;
        String[] mProjection = {MediaStore.Audio.Media._ID, MediaStore.Audio.Media.ALBUM_ID};
        String[] mArgs = {id};
        musicCursor = musicResolver.query(musicUri, mProjection, MediaStore.Audio.Media._ID + " = ?", mArgs, null);
        musicCursor.moveToFirst();
        Long albumId=musicCursor.getLong(musicCursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
        return albumId;
    }

答案 3 :(得分:0)

这就是我的方式。 效果很好。

String selection= MediaStore.Audio.Media.IS_MUSIC+"!=0";
    String[] projection={
            MediaStore.Audio.Media._ID,
            MediaStore.Audio.Media.DATA,
            MediaStore.Audio.Media.TITLE,
            MediaStore.Audio.Media.ARTIST,
            MediaStore.Audio.Media.ALBUM,
            MediaStore.Audio.Albums.ALBUM_ID,
            MediaStore.Audio.Media.DURATION
    };

    Cursor cursor=getActivity().managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,projection,selection,null,"title asc");
    List<String> data=new ArrayList<>();
    List<String> songs=new ArrayList<>();
    List<String> artists=new ArrayList<>();
    List<String> albums=new ArrayList<>();
    List<String> album_id=new ArrayList<>();
    List<String> durations=new ArrayList<>();

    cursor.moveToFirst();
    while (cursor.moveToNext()){
        if(cursor.getString(5).equals(mAlbumId)) {
            data.add(cursor.getString(1));
            songs.add(cursor.getString(2));
            artists.add(cursor.getString(3));
            albums.add(cursor.getString(4));
            album_id.add(mAlbumId);
            durations.add(cursor.getString(6));
        }
    }

答案 4 :(得分:0)

当你将cursor.movenext函数放入while循环时,第一行 MediaStore数据库将在不保存数据的情况下传递,因为该函数移动光标然后检查以便更好地创建值而不是将movenext函数放入while。

  try {
            Projection = new String[]{
            MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.ALBUM
            , MediaStore.Audio.Media.ARTIST,MediaStore.Audio.Media.DATA,                                    MediaStore.Audio.Media.SIZE};

            Cursor cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, Projection, null, null, null);

            cursor.moveToFirst();

            for (int i = 0; i < 5; i++) {
                columns[i] = cursor.getColumnIndex(Projection[i]);
            }

            Music tempMusic = new Music();

            while (next) {
                tempMusic.setName(cursor.getString(columns[0]));
                tempMusic.setAlbum(cursor.getString(columns[1]));
                tempMusic.setArtist(cursor.getString(columns[2]));
                tempMusic.setUri(cursor.getString(columns[3]));
                tempMusic.setSize(cursor.getDouble(columns[4]));
                if(!cursor.moveToNext())
                {
                    next=false;
                }
                musicss.add(tempMusic);

                tempMusic = new Music();
            }