Query songs of an album with CursorLoader

前端 未结 1 1066
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-25 10:22

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

相关标签:
1条回答
  • 2020-12-25 11:05

    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());
            }
    
        }
    }
    
    0 讨论(0)
提交回复
热议问题