java - 如果你有歌曲 ID,Android 从媒体商店获取歌曲?

标签 java android search cursor mediastore

我使用

从 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)

关于java - 如果你有歌曲 ID,Android 从媒体商店获取歌曲?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30005771/

相关文章:

java - 获取设备正常运行时间的正确方法是什么?

android - 如何处理 ImageView 上的向下滑动手势

java - 使用 JFileChooser 添加多个文件

java - 获取按用户排序的列表

c# - 在 Monodroid 中使用 Java 类文件

api - 使用正则表达式搜索 Twitter

javascript - 当列数据也是数组时,如何在数据表中应用 Vuetify 搜索

Java子字符串越界错误: Solution?

android - 将 View /布局膨胀到另一个布局?

bash - 我可以只 grep 文件的前 n 行吗?