与CursorLoader专辑的歌曲查询(Query songs of an album with

2019-06-25 12:55发布

我想通过查询获得的一张专辑的歌曲列表MediaStoreCursorLoader

我怎样才能做到这一点 ? 我能得到这个代码在设备上的所有曲目:

static final String[] TRACK_SUMMARY_PROJECTION = { MediaStore.Audio.Media._ID, MediaStore.Audio.Media.TITLE}; 

public Loader<Cursor> onCreateLoader(int id, Bundle args) {  
    String sortOrder = MediaStore.Audio.Media.TITLE + " ASC";
    String select = null;  
    return new CursorLoader(getActivity(), MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,  
                    TRACK_SUMMARY_PROJECTION, select, null,  
                    sortOrder);  
}  

我应该添加到代码或修改过滤特定专辑的歌曲?

Answer 1:

分步走

第1步看专辑的名字载入您的手机上

要请求光标唱片集信息

String[] columns = { android.provider.MediaStore.Audio.Albums._ID,
        android.provider.MediaStore.Audio.Albums.ALBUM };

cursor = managedQuery(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
        columns, null, null, null);

第2步一旦你找到所有的专辑names.You可以把它写下来想要的专辑名称,歌曲查询

请求包含用于特定专辑的歌曲信息光标

String[] columns = { MediaStore.Audio.Media.DATA,
          MediaStore.Audio.Media._ID,
          MediaStore.Audio.Media.TITLE,
          MediaStore.Audio.Media.DISPLAY_NAME,
          MediaStore.Audio.Media.MIME_TYPE, };

      String where = android.provider.MediaStore.Audio.Media.ALBUM
          + "=?";

      String whereVal[] = { Album name from which you want songs };

      String orderBy = android.provider.MediaStore.Audio.Media.TITLE;

      cursor = managedQuery(
          MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, columns,
          where, whereVal, orderBy);

现在回到这个光标。

供您参考下面是源代码检索专辑名称,并在它的所有歌曲。

package org.vipul;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;

public class HelloActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        String[] columns = { android.provider.MediaStore.Audio.Albums._ID,
                android.provider.MediaStore.Audio.Albums.ALBUM };

        Cursor cursor = managedQuery(
                MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, columns, null,
                null, null);

        if (cursor.moveToFirst()) {
            do {
                Log.v("Vipul",
                        cursor.getString(cursor
                                .getColumnIndex(android.provider.MediaStore.Audio.Albums.ALBUM)));
            } while (cursor.moveToNext());
        }

        // I want to list down song in album Rolling Papers (Deluxe Version)

        String[] column = { MediaStore.Audio.Media.DATA,
                MediaStore.Audio.Media._ID, MediaStore.Audio.Media.TITLE,
                MediaStore.Audio.Media.DISPLAY_NAME,
                MediaStore.Audio.Media.MIME_TYPE, };

        String where = android.provider.MediaStore.Audio.Media.ALBUM + "=?";

        String whereVal[] = { "Rolling Papers (Deluxe Version)" };

        String orderBy = android.provider.MediaStore.Audio.Media.TITLE;

        cursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                column, where, whereVal, orderBy);

        if (cursor.moveToFirst()) {
            do {
                Log.v("Vipul",
                        cursor.getString(cursor
                                .getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME)));
            } while (cursor.moveToNext());
        }

    }
}


文章来源: Query songs of an album with CursorLoader