Search code examples
androidrandomuriandroid-contentprovider

Android content provider random order


Can someone point me in the right direction please:

How can i provide this query back in random order?

musiccursor = getActivity().getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, 
musicdata, 
selection,
dirs, 
MediaColumns.DATA+ " asc");

I have tried replacing "asc" with various things, including Random(), RAND....i'm stumped


Solution

  • For the record, this is how i implemented the solution:

    if(isShuffle){
    // shuffle is on - play a random song
     sortOrder = "RANDOM()";
     } 
    else{
    sortOrder = MediaColumns.DATA+ " asc";   
    }
    
    ...
    
    //run the query          
    musiccursor = ctx.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, 
    musicdata, 
    selection,
    dirs,
    sortOrder);
    

    By doing this I can throw a randomly ordered cursor to a list view. The user gets to see the random play list order, rather than just randomly selecting the next song "in secret" that most apps do.