Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query songs of an album with CursorLoader

I'd like to get the list of songs of an album by querying the MediaStore with CursorLoader

How can i do this ? I can get all the songs of the device with this code :

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);  
}  

What should I add to the code or modification to filter songs of a particular Album ?

like image 637
Jecimi Avatar asked Jun 16 '12 16:06

Jecimi


1 Answers

Go step by step

Step 1 Look the names of albums loaded on you phone

To request cursor for Album information

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

Step 2 Once you find all album names.You can write down the desired album name and query songs from it

To request cursor containing song information for particular album

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

Now return this cursor.

For your reference below is Source code to retrieve Album name and all songs in it.

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());
        }

    }
}
like image 109
Vipul Avatar answered Nov 20 '22 16:11

Vipul