Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display All Song From the SD card Genre Wise

I am Doing Musicplayer Application. and want to show All the Songs with respect to its Genre. if possible then please give me some hint for that. i able to display all the Song With Respect to Artist and Album but Facing Problem While Going For Genre Wise Song. my out put is displaying all the Songs in Each genre catagory. it is not saprating the Song According to genre. Mycode is Below. LocalGenre.java

package com.PageViewerTilesDemo.src;

import java.util.ArrayList;

import android.app.Activity;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Window;
import android.widget.ExpandableListView;
import android.widget.TextView;

public class LocanGenre extends Activity {

    ExpandableListView listLocalArtists;
    TextView txttitle;
    Cursor musiccursor, musiccursor1;
    int music_column_index, music_column_index1;
    int count, count1;

    ArrayList<String> genresName = new ArrayList<String>();
    ArrayList<String> genreID = new ArrayList<String>();
    ArrayList<Integer> albumID = new ArrayList<Integer>();
    ArrayList<String> numberOFSongs = new ArrayList<String>();
    ArrayList<String> artistName = new ArrayList<String>();
    ArrayList<String> path = new ArrayList<String>();
    ArrayList<String> path12 = new ArrayList<String>();
    ArrayList<ArrayList<String>> pathDisplay = new ArrayList<ArrayList<String>>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.localartists);

        txttitle = (TextView) findViewById(R.id.title);

        txttitle.setText("Genres");

        listLocalArtists = (ExpandableListView) findViewById(R.id.listView1);

        init_phone_music_grid();

        listLocalArtists.setAdapter(new ExpandableListGenreAdapter(this, path, genresName,
                genresName, pathDisplay,albumID));
    }

    private void init_phone_music_grid() {
        // TODO Auto-generated method stub

        System.gc();

        String[] proj = {
                MediaStore.Audio.Media._ID,
                MediaStore.Audio.Media.DISPLAY_NAME,
                MediaStore.Audio.Media.ALBUM_ID};

        musiccursor1 = managedQuery(
                MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, proj, null, null,
                null);

        count1 = musiccursor1.getCount();

        if (count1 > 0) {
            musiccursor1.moveToFirst();
            do {

                music_column_index1 = musiccursor1
                        .getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);

                String filename0 = musiccursor1.getString(music_column_index1);
                path.add(filename0);

                Log.i("LocalGenres  ", "Path  Main" + path);

                music_column_index1 = musiccursor1
                        .getColumnIndexOrThrow(MediaStore.Audio.Media._ID);

                String filename123 = musiccursor1
                        .getString(music_column_index1);
                path12.add(filename123);

                Log.i("LocalGenre", "Media ID  " + path12);

                music_column_index1 = musiccursor1
                        .getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID);

                int filename1 = musiccursor1.getInt(music_column_index1);


                albumID.add(filename1);
                Log.i("LOCAL Genres!!!", " ALBUM ID" + albumID);


            } while (musiccursor1.moveToNext());
        }

        String[] projection = { MediaStore.Audio.Genres._ID,
                MediaStore.Audio.Genres.NAME};

        musiccursor = managedQuery(
                MediaStore.Audio.Genres.EXTERNAL_CONTENT_URI, projection, null,
                null, null);

        genresName.clear();

        count = musiccursor.getCount();

        if (count > 0) {
            musiccursor.moveToFirst();
            do {

                music_column_index = musiccursor
                        .getColumnIndexOrThrow(MediaStore.Audio.Genres._ID);

                String filename = musiccursor.getString(music_column_index);
                if(!genreID.contains(filename))
                {
                genreID.add(filename);
                }

                Log.i("Local Genres  ", "Genre ID" + genreID);

                music_column_index = musiccursor
                        .getColumnIndexOrThrow(MediaStore.Audio.Genres.NAME);

                String filename1 = musiccursor.getString(music_column_index);
                if(!genresName.contains(filename1))
                {
                genresName.add(filename1);
                }

                Log.i("Local Genres  ", "Genres Name  " + genresName);

                /*
                 * music_column_index = musiccursor
                 * .getColumnIndexOrThrow(MediaStore.Audio.Genres._COUNT);
                 * 
                 * String filename3 = musiccursor.getString(music_column_index);
                 * artistName.add(filename3);
                 * 
                 * Log.i("Local Albums  ", "Album ID for Gen  " + artistName);
                 */

            } while (musiccursor.moveToNext());
        }

        for (int j = 0; j < genreID.size(); j++) {

            ArrayList<String> arr = new ArrayList<String>();

            for (int i = 0; i < path12.size(); i++) {
                Log.i("EEEEEE", "Inside If path12.get(i) :"+path12.get(i));
                Log.i("EEEEEE", "Inside If genreID.get(j) :"+genreID.get(j));
                Log.i("EEEEEE", "Inside If Integer.parseInt(path12.get(i)) :"+Integer.parseInt(path12.get(i)));
                Log.i("EEEEEE", "Inside If j : "+j);
                if (path12.get(i).equalsIgnoreCase(genreID.get(j)) || Integer.parseInt(path12.get(i))>j) {

                    Log.i("EEEEEE", "Inside If");
                    arr.add(path.get(i));
                }

                else
                    Log.i("xxxxxxx", "Inside else");
                arr.add(path.get(i));
            }

            Log.i("EEEEEE", "Inside outerloop " + arr);

            pathDisplay.add(arr);
        }
    }
}

ExpandableListGenreAdapter.java

    package com.PageViewerTilesDemo.src;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ExpandableListGenreAdapter  extends BaseExpandableListAdapter{

    private Context context;

    private ArrayList<String> artist;

    private ArrayList<String> genres;

    private ArrayList<ArrayList<String>> children;

    public ArrayList<String> pathmain ;

    public ArrayList<Integer> genresID;

    public ArrayList<Integer> albumID;

    public ExpandableListGenreAdapter(Context context, ArrayList<String> path, ArrayList<String> groups,ArrayList<String> artist,
            ArrayList<ArrayList<String>> children, ArrayList<Integer> albumID) {
        this.context = context;
        this.genres = groups;
        this.artist = artist;
        this.pathmain = path;
        this.children = children;
        this.albumID=albumID;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return children.get(groupPosition).get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return childPosition;
    }

    /*public Bitmap getAlbumart(int album_id) 
       {
            Bitmap bm = null;
            try 
            {
                final Uri sArtworkUri = Uri
                    .parse("content://media/external/audio/albumart");

                Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);

                ParcelFileDescriptor pfd = context.getContentResolver()
                    .openFileDescriptor(uri, "r");

                if (pfd != null) 
                {
                    FileDescriptor fd = pfd.getFileDescriptor();
                    bm = BitmapFactory.decodeFileDescriptor(fd);
                }
        } catch (Exception e) {
        }
        return bm;
    }*/

    @Override
    public View getChildView(final int groupPosition, final int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        final String vehicle = (String) getChild(groupPosition, childPosition);

        Log.i("ExpandableListAdapter", "Group Position  "+groupPosition);

        Log.i("ExpandableListAdapter", "Vehicle  "+vehicle);

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.child_layout, null);
        }

        TextView tv = (TextView) convertView.findViewById(R.id.tvChild);
        ImageView imageview1=(ImageView)convertView.findViewById(R.id.ImageView01);
   //     bm=getAlbumart(albumids.get(1));
       // Log.i("LIST ADAPTER","@@@@@@@@@@@@@@@@@@@ALBUM IDS "+albumids.get(0)+"BITMAPPPPP@@@"+bm);

       // imageview1.setImageBitmap(coverart.get(childPosition));
        tv.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                MainActivity.flag = true;

                TestFragment3.flag = true;

                Firstpage.flag = true;

                Log.i("ExpandableListGenreAdapter", "path  "+childPosition);

                MainActivity.currentPosition = groupPosition;

                Log.i("ExpandableListGenreAdapter", "currentPosition  "+MainActivity.currentPosition);

                MainActivity.genre=true;
                MainActivity.currentgenreposition = albumID.get(childPosition);

                Log.i("ExpandableListGenreAdapter", "currentGenrePosition  "+MainActivity.currentgenreposition);

                MainActivity.Media_full_path = "/sdcard/"+vehicle;

                Log.i("ExpandableListAdapter", "Onclick  "+MainActivity.Media_full_path);

                ((Activity)context).finish();


            }
        });

        tv.setText("   " + vehicle.toString());

        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        // TODO Auto-generated method stub
        return children.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        // TODO Auto-generated method stub
        return genres.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        // TODO Auto-generated method stub
        return genres.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        // TODO Auto-generated method stub
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
         String group = (String) getGroup(groupPosition);
            if (convertView == null) {
                LayoutInflater infalInflater = (LayoutInflater) context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = infalInflater.inflate(R.layout.group_layout, null);
            }
            TextView txtArtistsName = (TextView) convertView.findViewById(R.id.txtArtistsName);
            TextView txtartistssongs = (TextView) convertView.findViewById(R.id.txtartistssongs);
            txtArtistsName.setText(group);
            txtartistssongs.setText(genres.get(groupPosition)+" Song(s)");
            return convertView;
    }

    @Override
    public boolean hasStableIds() {
        // TODO Auto-generated method stub
        return true;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return true;
    }

}

Please Suggest me Where is i am missing from the Above Code. Thank You.

like image 490
Android123 Avatar asked Dec 16 '22 02:12

Android123


1 Answers

Hi hope this will help you. It displays the genres and their songs.

int index;
long genreId;
Uri uri;
Cursor genrecursor;
Cursor tempcursor;
String[] proj1 = {MediaStore.Audio.Genres.NAME, MediaStore.Audio.Genres._ID};     
String[] proj2 = {MediaStore.Audio.Media.DISPLAY_NAME};

genrecursor = managedQuery(MediaStore.Audio.Genres.EXTERNAL_CONTENT_URI, proj1, null, null, null);
if (genrecursor.moveToFirst()) {
  do {
    index = genrecursor.getColumnIndexOrThrow(MediaStore.Audio.Genres.NAME);              
    Log.i("Tag-Genre name", genrecursor.getString(index));

    index = genrecursor.getColumnIndexOrThrow(MediaStore.Audio.Genres._ID);               
    genreId = Long.parseLong(genrecursor.getString(index));
    uri = MediaStore.Audio.Genres.Members.getContentUri("external", genreId);

    tempcursor = managedQuery(uri, proj2, null,null,null);
    Log.i("Tag-Number of songs for this genre", tempcursor.getCount() + "");
    if (tempcursor.moveToFirst()) {
      do {
        index = tempcursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
        Log.i("Tag-Song name", tempcursor.getString(index));
      } while(tempcursor.moveToNext());
    }
  } while(genrecursor.moveToNext());       
}
like image 91
Abhijeet Avatar answered Jan 09 '23 17:01

Abhijeet