use of com.simplecity.amp_library.model.Song in project Shuttle by timusus.
the class PlaylistUtils method createM3uPlaylist.
public static void createM3uPlaylist(final Context context, final Playlist playlist) {
ProgressDialog progressDialog = new ProgressDialog(context);
progressDialog.setIndeterminate(true);
progressDialog.setTitle(R.string.saving_playlist);
progressDialog.show();
playlist.getSongsObservable(context).flatMap(songs -> {
if (!songs.isEmpty()) {
File playlistFile = null;
if (Environment.getExternalStorageDirectory().canWrite()) {
File root = new File(Environment.getExternalStorageDirectory(), "Playlists/Export/");
if (!root.exists()) {
root.mkdirs();
}
File noMedia = new File(root, ".nomedia");
if (!noMedia.exists()) {
try {
noMedia.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
String name = playlist.name.replaceAll("[^a-zA-Z0-9.-]", "_");
playlistFile = new File(root, name + ".m3u");
int i = 0;
while (playlistFile.exists()) {
i++;
playlistFile = new File(root, name + i + ".m3u");
}
try {
FileWriter fileWriter = new FileWriter(playlistFile);
StringBuilder body = new StringBuilder();
body.append("#EXTM3U\n");
for (Song song : songs) {
body.append("#EXTINF:").append(song.duration / 1000).append(",").append(song.name).append(" - ").append(song.artistName).append("\n").append(song.path).append("\n");
}
fileWriter.append(body);
fileWriter.flush();
fileWriter.close();
} catch (IOException e) {
Log.e(TAG, "Failed to write file: " + e);
}
}
return Observable.just(playlistFile);
} else {
return Observable.empty();
}
}).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).doOnCompleted(progressDialog::dismiss).subscribe(file -> {
if (file != null) {
Toast.makeText(context, String.format(context.getString(R.string.playlist_saved), file.getPath()), Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, R.string.playlist_save_failed, Toast.LENGTH_SHORT).show();
}
});
}
Aggregations