List All Mp3 files in android

前端 未结 4 901
日久生厌
日久生厌 2021-02-03 16:28

I am developing music player in android but stuck in reading MP3 files. here is my code to read all mp3 files. but its not returing any files from device(although there are some

相关标签:
4条回答
  • 2021-02-03 16:42
    File[] files=new File(YOUR_DIR).listFiles();
    MediaMetadataRetriever mmr=new MediaMetadataRetriever();
    for (File file:files)
            {
                String fname=file.getName();
                String fpath=file.getAbsolutePath();
                if (file.isFile() && fname.contains("."))
                {
                    String ext=fname.substring(fname.lastIndexOf("."));
                    if (ext.equals(".mp3") || ext.equals(".MP3"))
                    {
                        mmr.setDataSource(fpath);
                        String title=mmr.extractMetadata(7);
                        String artist=mmr.extractMetadata(2);
                        String genre=mmr.extractMetadata(6);
                        String album=mmr.extractMetadata(1);
                    }
                }
    }
    mmr.release();
    
    0 讨论(0)
  • 2021-02-03 16:53

    Hope you already found your answer but may be this is better and if you wants to try, Here is your solution use the following code to Read the MP3 file from the Specific Folder or All files,

    First of all Create 1 Model class as Given Below, to GET and SET Files in list.

    AudioModel.class

    public class AudioModel {
    
        String aPath;
        String aName;
        String aAlbum;
        String aArtist;
    
        public String getaPath() {
            return aPath;
        }
    
        public void setaPath(String aPath) {
            this.aPath = aPath;
        }
    
        public String getaName() {
            return aName;
        }
    
        public void setaName(String aName) {
            this.aName = aName;
        }
    
        public String getaAlbum() {
            return aAlbum;
        }
    
        public void setaAlbum(String aAlbum) {
            this.aAlbum = aAlbum;
        }
    
        public String getaArtist() {
            return aArtist;
        }
    
        public void setaArtist(String aArtist) {
            this.aArtist = aArtist;
        }
    }
    

    Now We have our Model Class, use the below code to Read the all MP3 files from your Folder or device.

    This will return list of all MP3 Files with Music NAME, PATH, ARTIST, ALBUM. if you want more details, please refer the documentation for Media.Store.Audio

    public List<AudioModel> getAllAudioFromDevice(final Context context) {
    
            final List<AudioModel> tempAudioList = new ArrayList<>();
    
            Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            String[] projection = {MediaStore.Audio.AudioColumns.DATA, MediaStore.Audio.AudioColumns.ALBUM, MediaStore.Audio.ArtistColumns.ARTIST,};
            Cursor c = context.getContentResolver().query(uri, projection, MediaStore.Audio.Media.DATA + " like ? ", new String[]{"%yourFolderName%"}, null);
    
            if (c != null) {
                while (c.moveToNext()) {
    
                    AudioModel audioModel = new AudioModel();
                    String path = c.getString(0);
                    String album = c.getString(1);
                    String artist = c.getString(2);
    
                    String name = path.substring(path.lastIndexOf("/") + 1);
    
                    audioModel.setaName(name);
                    audioModel.setaAlbum(album);
                    audioModel.setaArtist(artist);
                    audioModel.setaPath(path);
    
                    Log.e("Name :" + name, " Album :" + album);
                    Log.e("Path :" + path, " Artist :" + artist);
    
                    tempAudioList.add(audioModel);
                }
                c.close();
            }
    
            return tempAudioList;
        }
    

    To read the files of a specific folder, use this query (write the target folder name in the query)

    Cursor c = context.getContentResolver().query(uri,
                                              projection, 
                                              MediaStore.Audio.Media.DATA + " like ? ", 
                                              new String[]{"%yourFolderName%"}, // yourFolderName 
                                              null);
    

    If you want all the files on the device, use this query:

    Cursor c = context.getContentResolver().query(uri,
                                              projection, 
                                              null, 
                                              null, 
                                              null);
    

    Don't forget to add the storage permission.

    0 讨论(0)
  • 2021-02-03 17:06

    here I've modified your getPlayList() method. look into it.

    ArrayList<HashMap<String,String>> getPlayList(String rootPath) {
                ArrayList<HashMap<String,String>> fileList = new ArrayList<>();
    
    
                try {
                    File rootFolder = new File(rootPath);
                    File[] files = rootFolder.listFiles(); //here you will get NPE if directory doesn't contains  any file,handle it like this.
                    for (File file : files) {
                        if (file.isDirectory()) {
                            if (getPlayList(file.getAbsolutePath()) != null) {
                                fileList.addAll(getPlayList(file.getAbsolutePath()));
                            } else {
                                break;
                            }
                        } else if (file.getName().endsWith(".mp3")) {
                            HashMap<String, String> song = new HashMap<>();
                            song.put("file_path", file.getAbsolutePath());
                            song.put("file_name", file.getName());
                            fileList.add(song);
                        }
                    }
                    return fileList;
                } catch (Exception e) {
                    return null;
                }
            }
    

    you can get the song name and song path like this:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);
    ArrayList<HashMap<String,String>> songList=getPlayList("/storage/sdcard1/");
            if(songList!=null){
            for(int i=0;i<songList.size();i++){
            String fileName=songList.get(i).get("file_name");
            String filePath=songList.get(i).get("file_path");
            //here you will get list of file name and file path that present in your device
            log.e("file details "," name ="+fileName +" path = "+filePath);
            }
            }
        }
    

    Note: use "/storage/sdcard1/" for reading files from sdCard and use Environment.getExternalStorageDirectory().getAbsolutePath() for reading files from phone memory

    Hope this will help you.

    0 讨论(0)
  • 2021-02-03 17:08

    Try This

    String path;
    File sdCardRoot = Environment.getExternalStorageDirectory();
    File dir = new File(sdCardRoot.getAbsolutePath() + "/yourDirectory/");
    
    if (dir.exists()) {
    
        if (dir.listFiles() != null) {
            for (File f : dir.listFiles()) {
                if (f.isFile())
                    path = f.getName();
    
                if (path.contains(".mp3")) {
                    yourArrayList.add(path);
    
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题