Search code examples
androidaudiomp3

How can I fetch the Audio files from Android Device above Android 4.0 like Whatsapp?


enter image description here

I want to show track same as Image and want to play it there.

Using below code I can try to fetch Mp3 files but can't play that file there.

So please help me..

     public void openGalleryAudio(){
      Intent intent = new Intent();
      intent.setType("audio/*");
      intent.setAction(Intent.ACTION_GET_CONTENT);
      startActivityForResult(Intent.createChooser(intent,"Select Audio "), SELECT_AUDIO);
     }

Solution

  • Using this method I can Find the detail of all Mp3 files from whole sdcard. In this Track_Bean is a Get and Set Method of Different String variables.

    For Example

    public class Track_Bean {
        private String mTitle = "";
    
    public String getmTitle() {
        return mTitle;
    }
    
    public void setmTitle(String mTitle) {
        this.mTitle = mTitle;
     }
    }
    

    ================================******==========================

     /**
    * SD CARD QUERIES
    */
    public ArrayList<Track_Bean> getAllSdCardTrack_Beans(Context context) {
    ArrayList<Track_Bean> Track_Beans = new ArrayList<Track_Bean>();
    
        Cursor c = context
                .getContentResolver()
                .query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                        new String[] { MediaStore.Audio.Media._ID,
                                MediaStore.Audio.Media.DATA,
                                MediaStore.Audio.Media.ARTIST,
                                MediaStore.Audio.Media.ALBUM,
                                MediaStore.Audio.Media.DURATION,
                                MediaStore.Audio.Media.DISPLAY_NAME,
                                MediaStore.Audio.Media.ALBUM_ID }, "1=1",
                        null, null);
        if (c.moveToFirst()) {
            do {
                Track_Bean mTcBean = new Track_Bean();
    
    
                 String mTitle = c
                         .getString(c
                                 .getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME));
    
                 if(mTitle.trim().endsWith(".mp3") || mTitle.trim().endsWith(".MP3")){
                     mTcBean.setmTitle(mTitle);
    
                     String mArtist = c
                             .getString(c
                                     .getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
                     mTcBean.setmArtist(mArtist);
    
                     //Media Id
                     String mId = c.getString(c
                             .getColumnIndexOrThrow(MediaStore.Audio.Media._ID));
                     mTcBean.setmId(mId);
                     //ALbumName
                     String mAlbumName = c
                             .getString(c
                                     .getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
                     mTcBean.setmAlbumName(mAlbumName);
    
                     String mAlbumID = c
                             .getString(c
                                     .getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));
                     mTcBean.setmAlbumId(mAlbumID);
                     //Path
                     String mPath = c
                             .getString(c
                                     .getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
                     mTcBean.setmPath(mPath);
                     System.out.println("PATH"+mPath);
                    //Duration
                     long mDuration = c
                             .getLong(c
                                     .getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));
                     String mFormattedDuration = DateUtils
                             .formatElapsedTime(mDuration / 1000);
                     mTcBean.setmDuration(mFormattedDuration);
    
                     Track_Beans.add(mTcBean);
                 }else{
                     System.out.println("The Track ext is not Mp3::"+mTitle);
                 }
    
            } while (c.moveToNext());
            if (c != null)
                c.close();
        }
        System.out.println("Size of array::"+Track_Beans.size());
        mLv_tracks.setAdapter(new Audio_Post_Adapter(Audio_Fetch.this, Track_Beans));
    return Track_Beans;
    
     }