public static ArrayList<Playlist> getAllPlaylists( Context context ){
ArrayList<Playlist> playlists = new ArrayList<Playlist>();
ArrayList<String> fileList = new ArrayList<>();
//this will get me all playlist files available for my application
for( String file : context.fileList() )
if( file.endsWith(".playlist") )
fileList.add(file);
for( String file : fileList ){
Playlist newPlaylist = loadPlaylist( context, file );
if( !newPlaylist.isEmpty() ){
playlists.add(newPlaylist);
}
}
return playlists;
}
private static Playlist loadPlaylist( Context context, String file ){
Playlist playlist = new Playlist();
try {
FileInputStream inputStream = context.openFileInput(file);
byte[] inputBuffer = new byte[ inputStream.available() ];
inputStream.read(inputBuffer);
String content = new String(inputBuffer);
//each line holds a path so we split the content
String[] files = content.split("\n");
playlist.setName( files[0] );
for( int i = 1; i < files.length; i++ ){
//This will just load the Song (artist, title etc.)
//if somehow the path leads to nowhere or it is just not a
//music file, getSong() will return null
Song song = MediaManager.getSong( context, files[i] );
if( song != null )
playlist.addSong( song );
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return playlist;
}
The structure I chose for my playlist files is pretty simple. The first line is the name of that playlist and the following lines will contain a filepath which I can use later on to load a file and play it / display its data.
Here is an example of what they might look like :
My first Playlist
/sdcard/.../.../whatever.mp3
/sdcard/.../.../noway.mp3
/sdcard/.../.../seriously.mp3
How can I improve the speed of this process? It takes about 1 second for 1 file...
AsyncTask
so that the operation can be performed in the background without making your application not responding. \$\endgroup\$