2

First of all i am a newbie,here is the code for adding the song's name in the customlost ,all i need is just some function using mediastore(i tried metadataretriever but the app stopped responding ) so that i can add them too....

public class FragmentSongs extends Fragment {

    private MediaPlayer mp =new MediaPlayer();



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_a, container, false);
        ListView SngList = (ListView) view.findViewById(R.id.SongList);
        File f=new File("/sdcard");
        ArrayList<SongDetails> Songinfo = getSongsFromDirectory(f);
        if (Songinfo.size()>0){
           SngList.setAdapter(new CustomAdapter(Songinfo));
           return view;
        }
        else return null;

    }


        public ArrayList<SongDetails> getSongsFromDirectory(File f)
        {// MediaMetadataRetriever metaRetriever = new MediaMetadataRetriever();

            ArrayList<SongDetails> songs = new ArrayList<SongDetails>();
            if (!f.exists() || !f.isDirectory()) 

            {    
                return songs;
            }
            File[] files = f.listFiles(new Mp3Filter());
            for(int i=0; i<files.length; i++) 
            { 

            if (files[i].isFile()){
          //    metaRetriever.setDataSource(files[i].getName());
            //metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST);
            SongDetails detail=new SongDetails(); 
            detail.setIcon(R.drawable.ic_launcher); 
            String fileName = files[i].getName(); 
            detail.setSong(files[i].getName()); 

            detail.setArtist(files[i].getName()); 
            detail.setAlbum(files[i].getName()); 
            songs.add(detail); 
            }else if (files[i].isDirectory()){ 
            songs.addAll(getSongsFromDirectory(files[i])); 
            } 

            }

            return songs;



    } }
class Mp3Filter implements FileFilter{
    public boolean accept(File file){
       return (file.isDirectory()||file.getName().endsWith(".MP3")|| file.getName().endsWith(".mp3"));
    }

1 Answers1

1

Not sure what the precise question is but if you are looking to add song titles to a custom list, why not use something like this

    final String track_id = MediaStore.Audio.Media._ID; 
    final String track_no =MediaStore.Audio.Media.TRACK;
    final String 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;
    final String composer = MediaStore.Audio.Media.COMPOSER;
    final String year = MediaStore.Audio.Media.YEAR;
    final String path = MediaStore.Audio.Media.DATA;
    Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
public Cursor getTrackTrackcursor(Context context, Cursor cursor)
{
    // gets all tracks
    ContentResolver cr =  context.getContentResolver();
    final String[]columns={track_id, track_no, artist, track_name,album, duration, path, year, composer};
    cursor = cr.query(uri,columns,null,null,null);
    return cursor;
}

Hope this helps

Theo
  • 2,012
  • 1
  • 16
  • 29
  • well thanx for your time,but how can i add it it my list? –  Aug 04 '13 at 10:15
  • Once you have your cursor you loop through it by going cursor.movefirst(), cursor.movenext() etc. Each loop you add the data cursor.getString() to your adapter listitems.Let me know if you are stuck. – Theo Aug 04 '13 at 19:18
  • Can you explain the code little further. What If I wan to implement this cursor inside the fragment – Prashant Patel Apr 09 '14 at 13:55