列出android中的所有MP3文件 [英] List All Mp3 files in android
问题描述
我正在用android开发音乐播放器,但无法读取MP3文件.这是我的代码,可读取所有mp3文件.但它不会重现设备上的任何文件(尽管有些文件是我使用adb复制的).我也希望它使用专辑,艺术家等列出.请在此方面提供帮助.
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 files which i copied using adb). I also want it to list using Album, artist etc. please help in this.
final String MEDIA_PATH = Environment.getExternalStorageDirectory()+"";
private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
// Constructor
public SongsManager(){
}
/**
* Function to read all mp3 files from sdcard
* and store the details in ArrayList
* */
public ArrayList<HashMap<String, String>> getPlayList(){
File home = new File(MEDIA_PATH);
//if (home.listFiles(new FileExtensionFilter()).length > 0) {
if (home.listFiles(new FileExtensionFilter())!=null) {
for (File file : home.listFiles(new FileExtensionFilter())) {
HashMap<String, String> song = new HashMap<String, String>();
song.put("songTitle", file.getName().substring(0, (file.getName().length() - 4)));
song.put("songPath", file.getPath());
// Adding each song to SongList
songsList.add(song);
}
}
// return songs list array
return songsList;
}
/**
* Class to filter files which are having .mp3 extension
* */
class FileExtensionFilter implements FilenameFilter {
public boolean accept(File dir, String name) {
return (name.endsWith(".mp3") || name.endsWith(".MP3"));
}
}
推荐答案
在这里,我修改了您的getPlayList()方法.看看它.
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);
}
}
}
注意:使用"/storage/sdcard1/"从sdCard中读取文件,并使用Environment.getExternalStorageDirectory().getAbsolutePath()从手机内存中读取文件
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.
这篇关于列出android中的所有MP3文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!