簡體   English   中英

如果您有歌曲ID,Android會從Media Store獲取歌曲嗎?

[英]Android get song from Media Store if you have the song ID?

我從MediaStore的播放列表中獲得了歌曲ID,使用

long id = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Playlists.Members.AUDIO_ID));

並且ID是正確的,但由於僅有的其他可用數據是CONTENT_DIRECTORY,DEFAULT_SORT_ORDER,PLAYLIST_ID,PLAY_ORDER和_ID,因此我不確定如何獲取歌曲的重要部分。 我需要標題,專輯,藝術家等,就好像我正在通過MediaStore.Audio.Media獲取歌曲信息一樣。

我找到了一個嘗試修改的答案以滿足自己的需求,但是我不太了解查詢或游標,因此我不確定是否有獲取歌曲的方法。

如果這樣做的唯一方法是在每首歌曲中循環播放,直到找到匹配的ID,我就可以做到這一點,但是這樣做效率低下,必須有更好的方法。

提前致謝!

我可以使用以下代碼通過ID查詢歌曲:

    Uri mediaContentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    String[] projection = new String[] { MediaStore.Audio.Media._ID, MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.ALBUM,
            MediaStore.Audio.Media.DURATION, MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.ALBUM_ID};
    String selection = MediaStore.Audio.Media._ID + "=?";
    String[] selectionArgs = new String[] {"" + id}; //This is the id you are looking for

    Cursor mediaCursor = getContentResolver().query(mediaContentUri, projection, selection, selectionArgs, null);

    if(mediaCursor.getCount() >= 0) {
        mediaCursor.moveToPosition(0);
        String title = mediaCursor.getString(mediaCursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
        String album = mediaCursor.getString(mediaCursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));
        String artist = mediaCursor.getString(mediaCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
        long duration = mediaCursor.getLong(mediaCursor.getColumnIndex(MediaStore.Audio.Media.DURATION));

        //Do something with the data
    }

您可以通過更改selection和selectionArgs來查詢其他內容。 這是查詢功能的文檔:

  /* 
  Query the given URI, returning a {@link Cursor} over the result set.
  For best performance, the caller should follow these guidelines:
   - Provide an explicit projection, to prevent reading data from storage that aren't going to be used.
   - Use question mark parameter markers such as 'phone=?' instead of explicit values in the {@code selection} parameter, so that queries that differ only by those values will be recognized as the same for caching purposes.

   @param uri The URI, using the content:// scheme, for the content to retrieve.
   @param projection A list of which columns to return. Passing null will return all columns, which is inefficient.
   @param selection A filter declaring which rows to return, formatted as an SQL WHERE clause (excluding the WHERE itself). Passing null will return all rows for the given URI.
   @param selectionArgs You may include ?s in selection, which will be replaced by the values from selectionArgs, in the order that they appear in the selection. The values will be bound as Strings.
   @param sortOrder How to order the rows, formatted as an SQL ORDER BY clause (excluding the ORDER BY itself). Passing null will use the default sort order, which may be unordered.
   @return A Cursor object, which is positioned before the first entry, or null
   @see Cursor
 */

  public final Cursor query(Uri uri, String[] projection,
        String selection, String[] selectionArgs, String sortOrder)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM