Search code examples
androidandroid-mediaplayermedia-player

How do I get songs of particular album?


I'm trying to get songs of a particular album, so I tried some answer on Stack Overflow, but the answer didn't work. I compared it with IDs, but it didn't help me. So, please help me. How do I do this? I'm getting albums listed, but when I click on those I'm getting all the songs present on emulator. So how I seperate according to album?

public class albumSongs extends AppCompatActivity implements SearchView.OnQueryTextListener {

    ArrayList<albumInfo>albumSongList=new ArrayList<>();
    RecyclerView recyclerView2;
    public albumsongAdapter albumsongAdapter;
     String albumid;
    String track_name;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_album_songs);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("try");
        getSupportActionBar().setDisplayShowTitleEnabled(true);
        recyclerView2 = (RecyclerView) findViewById(R.id.recyclerView2);
        recyclerView2.setHasFixedSize(true);
        albumsongAdapter = new albumsongAdapter(albumSongList,this);
        SearchView searchView=(SearchView)findViewById(R.id.search);
        recyclerView2.setAdapter(albumsongAdapter);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView2.getContext(),
                linearLayoutManager.getOrientation());
        recyclerView2.setLayoutManager(linearLayoutManager);
        recyclerView2.addItemDecoration(dividerItemDecoration);
        getSongsPerAlbum("");
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        final MenuItem item = menu.findItem(R.id.search_view);
        final SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
        searchView.setOnQueryTextListener(this);
        searchView.setQueryHint("search songs");
        MenuItemCompat.setOnActionExpandListener(item,new MenuItemCompat.OnActionExpandListener() {
            @Override
            public boolean onMenuItemActionExpand(MenuItem menuItem) {
                // Return true to allow the action view to expand
                return true;
            }
            @Override
            public boolean onMenuItemActionCollapse(MenuItem menuItem) {
                // When the action view is collapsed, reset the query
                albumsongAdapter.setSearchResult(albumSongList);
                // Return true to allow the action view to collapse
                return true;
            }
        });
        return true;
    }
    public boolean onQueryTextSubmit(String query) {
        return false;
    }

    public boolean onQueryTextChange(String newText) {
        final List<albumInfo> filteredModelList = filter(albumSongList, newText);
        albumsongAdapter.setSearchResult(filteredModelList);
        return true;
    }


    private List<albumInfo> filter(ArrayList<albumInfo> models, String query) {
        query = query.toLowerCase();
        final List<albumInfo> filteredModelList = new ArrayList<>();
        for (albumInfo model : models) {
            final String text = model.getX().toLowerCase();
            if (text.contains(query)) {
                filteredModelList.add(model);
            }
        }
        return filteredModelList;
    }
    public void getSongsPerAlbum(String AlbumId) {
        final Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        final String track_id = MediaStore.Audio.Media._ID;
        final String track_no = MediaStore.Audio.Media.TRACK;
        track_name = MediaStore.Audio.Media.TITLE;
        final String artist = MediaStore.Audio.Media.ARTIST;
        final String duration = MediaStore.Audio.Media.DURATION;
        final String album = MediaStore.Audio.Media.ALBUM;
        albumid = MediaStore.Audio.Media.ALBUM_ID;
        final String composer = MediaStore.Audio.Media.COMPOSER;
        final String year = MediaStore.Audio.Media.YEAR;
        final String path = MediaStore.Audio.Media.DATA;
        final String date_added = MediaStore.Audio.Media.DATE_ADDED;
        String[] columns = new String[]{
                track_id,
                track_no,
                track_name,
                artist,
                duration,
                album,
                albumid,
                composer,
                year,
                path,
                date_added
        };
        // Get all tracks if no album id
        Cursor csr;
        if (AlbumId == null || AlbumId.length() < 1) {
            csr = this.getContentResolver().query(uri, columns,null,null,track_no);
        } else {
            // Doesn't work think media has to be scanned first before albums are generated
            csr = this.getContentResolver().query(uri, columns,album + "=?",new String[]{AlbumId},track_no);
        }

        for (String s: csr.getColumnNames()
                ) {
            Log.d("SONGCOLUMN","Column = " + s);
        }
        while (csr.moveToNext()) {
            String id = (csr.getString(csr.getColumnIndex(track_name)));
            String thisalbumId=(csr.getString(csr.getColumnIndex(path)));
            String v = (csr.getString(csr.getColumnIndex(albumid)));
            Log.d("SONG","Name of Song is " + csr.getString(csr.getColumnIndex(track_no)) +
                    " from Album " + csr.getString(csr.getColumnIndex(albumid)));


                albumInfo s = new albumInfo(id, v, thisalbumId);
                albumSongList.add(s);

        }
        csr.close();
        //  recyclerView2.setAdapter(albumsongAdapter);
    }


}

Solution

  • i did it like this.

    public void songs()
    {
        Uri uri=MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;
    
        String[] columns = { android.provider.MediaStore.Audio.Albums._ID,
                android.provider.MediaStore.Audio.Albums.ALBUM };
    
        Cursor cursor =getApplicationContext().getContentResolver().
                query(uri, columns, null,
                        null, null);
    
        if (cursor != null && cursor.moveToFirst()) {
            do {
                Log.v("Vipul",
                        cursor.getString(cursor
                                .getColumnIndex(MediaStore.Audio.Albums.ALBUM)));
                v=cursor.getString(cursor
                        .getColumnIndex(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.ALBUM_ID, MediaStore.Audio.Media.TITLE,
                MediaStore.Audio.Media.DISPLAY_NAME,
                MediaStore.Audio.Media.MIME_TYPE, };
    
        String where = android.provider.MediaStore.Audio.Media.ALBUM + "=?";
        Log.e("syn",v);
        String whereVal[] = {v};
    
        String orderBy = android.provider.MediaStore.Audio.Media.TITLE;
    
        cursor =getApplicationContext().getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                column,where,whereVal, orderBy);
    
        if (cursor != null && cursor.moveToFirst() ) {
            do {
                Log.v("Vipul",
                        cursor.getString(cursor
                                .getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME)));
                String name=cursor.getString(cursor
                        .getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
                String art=cursor.getString(cursor
                        .getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
                String path=cursor.getString(cursor
                        .getColumnIndex(MediaStore.Audio.Media.DATA));
    
    
                albumInfo s = new albumInfo(name,art,path); // EXCLUDED
                albumSongList.add(s);
    
            } while (cursor.moveToNext());
        }
    }