tbp
tbp

Reputation: 131

Unable to get audio list from content provider

I am building a music player and songs are being displayed multiple times in logcat. I want to display the songs in listview. Here is my main activity:

public class MainActivity extends AppCompatActivity {
    List<String> list=new ArrayList<>();
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            File s= Environment.getExternalStorageDirectory();
            String[] as={"is_music","title"};
            Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            Cursor musicCursor = getContentResolver().query(musicUri,as, null, null,null);
            String[] a=musicCursor.getColumnNames();
            musicCursor.moveToFirst();
            Log.i ("fjfj",""+musicCursor.getCount());
            for(int i=0;i<musicCursor.getCount();i++){        
               list.add(musicCursor.getString(musicCursor.getColumnIndex("title")));
               //Log.i("position",musicCursor.getPosition()+list.get(musicCursor.getPosition()));
               musicCursor.moveToNext();        
               Log.i("column",i+" "+list.get(i));
            }

        }
    }

03-08 21:17:26.672 2070-2070/com.example.android.alpha1 I/column: 0 Welcome To New York 03-08 21:17:26.676 2070-2070/com.example.android.alpha1 I/column: 1 Blank Space 03-08 21:17:26.688 2070-2070/com.example.android.alpha1 I/column: 2 Style 03-08 21:17:26.688 2070-2070/com.example.android.alpha1 I/column: 3 Out Of The Woods 03-08 21:17:26.692 2070-2070/com.example.android.alpha1 I/column: 4 All You Had To Do Was Stay 03-08 21:17:26.696 2070-2070/com.example.android.alpha1 I/column: 5 Shake It Off 03-08 21:17:26.696 2070-2070/com.example.android.alpha1 I/column: 6 I Wish You Would 03-08 21:17:26.700 2070-2070/com.example.android.alpha1 I/column: 7 Bad Blood 03-08 21:17:26.704 2070-2070/com.example.android.alpha1 I/column: 8 Wildest Dreams 03-08 21:17:26.704 2070-2070/com.example.android.alpha1 I/column: 9 How You Get The Girl 03-08 21:17:26.712 2070-2070/com.example.android.alpha1 I/column: 10 This Love 03-08 21:17:26.712 2070-2070/com.example.android.alpha1 I/column: 11 I Know Places 03-08 21:17:26.712 2070-2070/com.example.android.alpha1 I/column: 12 Clean 03-08 21:17:26.720 2070-2070/com.example.android.alpha1 I/column: 13 Wonderland 03-08 21:17:26.724 2070-2070/com.example.android.alpha1 I/column: 14 You Are In Love 03-08 21:17:26.728 2070-2070/com.example.android.alpha1 I/column: 15 New Romantics 03-08 21:17:26.732 2070-2070/com.example.android.alpha1 I/column: 16 I Know Places (Voice Memo) 03-08 21:17:26.744 2070-2070/com.example.android.alpha1 I/column: 17 I Wish You Would (Voice Memo) 03-08 21:17:26.748 2070-2070/com.example.android.alpha1 I/column: 18 Blank Space (Voice Memo) 03-08 21:17:26.752 2070-2070/com.example.android.alpha1 I/column: 19 Welcome To New York 03-08 21:17:26.752 2070-2070/com.example.android.alpha1 I/column: 20 Blank Space 03-08 21:17:26.756 2070-2070/com.example.android.alpha1 I/column: 21 Style 03-08 21:17:26.760 2070-2070/com.example.android.alpha1 I/column: 22 Out Of The Woods 03-08 21:17:26.764 2070-2070/com.example.android.alpha1 I/column: 23 All You Had To Do Was Stay 03-08 21:17:26.764 2070-2070/com.example.android.alpha1 I/column: 24 Shake It Off 03-08 21:17:26.764 2070-2070/com.example.android.alpha1 I/column: 25 I Wish You Would

Upvotes: 0

Views: 1186

Answers (1)

WideFide
WideFide

Reputation: 345

Do it this way. Initialize ArrayList and Call a function in onCreate().

Before onCreate()

private ArrayList<String> songArrayList;
private ListView listView;

In OnCreate():

songArrayList = new ArrayList();
listview = (*Find view by Id *);
getSongList();
// Now create and set adapter to listview. Use songArrayList.

Function:

public void getSongList()
{
    /**
     * All the audio files can be accessed using the below initialised musicUri.
     * And there is a cursor to iterate over each and every column.
     */
    ContentResolver contentResolver = getActivity().getContentResolver();
    Uri musicUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    Cursor musicCursor = contentResolver.query(musicUri, null, null, null, null, null);

    // If cursor is not null
    if(musicCursor != null && musicCursor.moveToFirst())
    {
        //get Columns
        int titleColumn = musicCursor.getColumnIndex(MediaStore.Audio.Media.TITLE);


        // Store the title, id and artist name in Song Array list.
        do
        {
            String thisTitle = musicCursor.getString(titleColumn);
            // Add the info to our array.
            songArrayList.add(thisTitle);
        }
        while (musicCursor.moveToNext());

        // For best practices, close the cursor after use.
        musicCursor.close();
    }
}

This is a sample example taken from my current ongoing project. Hope this helps.

Upvotes: 1

Related Questions