简体   繁体   English

如果您有歌曲ID,Android会从Media Store获取歌曲吗?

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

I got a song id from a playlist in MediaStore, using 我从MediaStore的播放列表中获得了歌曲ID,使用

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

and the id is correct, but as the only other data available is CONTENT_DIRECTORY, DEFAULT_SORT_ORDER, PLAYLIST_ID, PLAY_ORDER, and _ID, I am not sure how to get the important parts of the song. 并且ID是正确的,但由于仅有的其他可用数据是CONTENT_DIRECTORY,DEFAULT_SORT_ORDER,PLAYLIST_ID,PLAY_ORDER和_ID,因此我不确定如何获取歌曲的重要部分。 I need the title, album, artist, etc., as if I was going through MediaStore.Audio.Media to get Song info. 我需要标题,专辑,艺术家等,就好像我正在通过MediaStore.Audio.Media获取歌曲信息一样。

I found an answer that I tried to modify to fit my needs, but I don't really understand querying or cursors, I am not sure how if there is a way to get a song. 我找到了一个尝试修改的答案以满足自己的需求,但是我不太了解查询或游标,因此我不确定是否有获取歌曲的方法。

If the only way to do this is to cycle through every single song until i find a matching ID i can do that, but it is wildly inefficient and there has to be a better way. 如果这样做的唯一方法是在每首歌曲中循环播放,直到找到匹配的ID,我就可以做到这一点,但是这样做效率低下,必须有更好的方法。

Thanks in advance! 提前致谢!

I was able to query a song by ID using the following code: 我可以使用以下代码通过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
    }

You can query for different things by changing the selection and selectionArgs. 您可以通过更改selection和selectionArgs来查询其他内容。 Here is the documentation from the query function: 这是查询功能的文档:

  /* 
  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