首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果你有歌曲ID,Android会从媒体商店得到歌曲吗?

如果你有歌曲ID,Android会从媒体商店得到歌曲吗?
EN

Stack Overflow用户
提问于 2015-05-02 18:11:12
回答 1查看 3.8K关注 0票数 2

我从MediaStore中的播放列表中获得一个歌曲id,使用

代码语言:javascript
复制
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,我可以这样做,但它是非常低效率,必须有一个更好的方法。

提前感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-05-14 00:02:45

我能够使用以下代码按ID查询歌曲:

代码语言:javascript
复制
    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
    }

您可以通过更改选择和selectionArgs来查询不同的内容。以下是查询函数的文档:

代码语言:javascript
复制
  /* 
  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)
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30005771

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档