Search in sources :

Example 26 with Song

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();
        }
    });
}
Also used : Song(com.simplecity.amp_library.model.Song) SpannableStringBuilder(android.text.SpannableStringBuilder) FileWriter(java.io.FileWriter) IOException(java.io.IOException) ProgressDialog(android.app.ProgressDialog) File(java.io.File)

Aggregations

Song (com.simplecity.amp_library.model.Song)26 List (java.util.List)14 Intent (android.content.Intent)13 Stream (com.annimon.stream.Stream)12 Album (com.simplecity.amp_library.model.Album)12 Context (android.content.Context)11 ArrayList (java.util.ArrayList)11 Observable (rx.Observable)11 Toast (android.widget.Toast)10 Collectors (com.annimon.stream.Collectors)10 R (com.simplecity.amp_library.R)10 AndroidSchedulers (rx.android.schedulers.AndroidSchedulers)10 Schedulers (rx.schedulers.Schedulers)10 Uri (android.net.Uri)9 AlbumArtist (com.simplecity.amp_library.model.AlbumArtist)9 Playlist (com.simplecity.amp_library.model.Playlist)9 ContentUris (android.content.ContentUris)7 MediaStore (android.provider.MediaStore)7 View (android.view.View)7 Glide (com.bumptech.glide.Glide)7