I have this method that returns all files by extension to a folder inside the resource folder:
public static String[] getAllFilesInAssetByExtension(Context context, String path, String extension){ Assert.assertNotNull(context); try { String[] files = context.getAssets().list(path); if(StringHelper.isNullOrEmpty(extension)){ return files; } List<String> filesWithExtension = new ArrayList<String>(); for(String file : files){ if(file.endsWith(extension)){ filesWithExtension.add(file); } } return filesWithExtension.toArray(new String[filesWithExtension.size()]); } catch (IOException e) {
if you call it using:
getAllFilesInAssetByExtension(yourcontext, "", ".mp3");
this will return all my mp3 files in the root folder with resources.
if you call it using:
getAllFilesInAssetByExtension(yourcontext, "somefolder", ".mp3");
it will search in "somefolder" for mp3 files
Now that you have a list of all the files you are opening, you will need the following:
AssetFileDescriptor descriptor = getAssets().openFd("myfile");
To play the file, simply do:
MediaPlayer player = new MediaPlayer(); long start = descriptor.getStartOffset(); long end = descriptor.getLength(); player.setDataSource(this.descriptor.getFileDescriptor(), start, end); player.prepare(); player.setVolume(1.0f, 1.0f); player.start();
Hope this helps
source share