Added option to save a playlist as m3u file.
This commit is contained in:
parent
1b3d98e098
commit
1aa6aec9cb
6 changed files with 106 additions and 3 deletions
|
|
@ -0,0 +1,8 @@
|
|||
package com.kabouzeid.gramophone.helper;
|
||||
|
||||
public interface M3UConstants {
|
||||
String EXTENSION = "m3u";
|
||||
String HEADER = "#EXTM3U";
|
||||
String ENTRY = "#EXTINF:";
|
||||
String DURATION_SEPARATOR = ",";
|
||||
}
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
package com.kabouzeid.gramophone.helper;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.kabouzeid.gramophone.loader.PlaylistSongLoader;
|
||||
import com.kabouzeid.gramophone.model.Playlist;
|
||||
import com.kabouzeid.gramophone.model.PlaylistSong;
|
||||
import com.kabouzeid.gramophone.model.Song;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class M3UWriter implements M3UConstants {
|
||||
public static final String TAG = M3UWriter.class.getSimpleName();
|
||||
|
||||
public static File write(Context context, File dir, Playlist playlist) throws IOException {
|
||||
if (!dir.exists()) //noinspection ResultOfMethodCallIgnored
|
||||
dir.mkdirs();
|
||||
File file = new File(dir, playlist.name.concat("." + EXTENSION));
|
||||
|
||||
ArrayList<PlaylistSong> songs = PlaylistSongLoader.getPlaylistSongList(context, playlist.id);
|
||||
|
||||
if (songs.size() > 0) {
|
||||
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
|
||||
|
||||
bw.write(HEADER);
|
||||
for (Song song : songs) {
|
||||
bw.newLine();
|
||||
bw.write(ENTRY + song.duration + DURATION_SEPARATOR + song.artistName + " - " + song.title);
|
||||
bw.newLine();
|
||||
bw.write(song.data);
|
||||
}
|
||||
|
||||
bw.close();
|
||||
}
|
||||
|
||||
return file;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,9 +1,13 @@
|
|||
package com.kabouzeid.gramophone.helper.menu;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.os.AsyncTask;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.view.MenuItem;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.kabouzeid.gramophone.R;
|
||||
import com.kabouzeid.gramophone.dialogs.DeletePlaylistDialog;
|
||||
|
|
@ -13,7 +17,9 @@ import com.kabouzeid.gramophone.loader.PlaylistSongLoader;
|
|||
import com.kabouzeid.gramophone.model.Playlist;
|
||||
import com.kabouzeid.gramophone.model.Song;
|
||||
import com.kabouzeid.gramophone.model.smartplaylist.AbsSmartPlaylist;
|
||||
import com.kabouzeid.gramophone.util.PlaylistsUtil;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
|
|
@ -22,7 +28,7 @@ import java.util.ArrayList;
|
|||
public class PlaylistMenuHelper {
|
||||
public static final int MENU_RES = R.menu.menu_item_playlist;
|
||||
|
||||
public static boolean handleMenuClick(@NonNull AppCompatActivity activity, @NonNull Playlist playlist, @NonNull MenuItem item) {
|
||||
public static boolean handleMenuClick(@NonNull AppCompatActivity activity, @NonNull final Playlist playlist, @NonNull MenuItem item) {
|
||||
switch (item.getItemId()) {
|
||||
case R.id.action_play:
|
||||
MusicPlayerRemote.openQueue(new ArrayList<>(getPlaylistSongs(activity, playlist)), 0, true);
|
||||
|
|
@ -36,6 +42,35 @@ public class PlaylistMenuHelper {
|
|||
case R.id.action_delete_playlist:
|
||||
DeletePlaylistDialog.create(playlist).show(activity.getSupportFragmentManager(), "DELETE_PLAYLIST");
|
||||
return true;
|
||||
case R.id.action_save_playlist:
|
||||
@SuppressLint("ShowToast") final Toast toast = Toast.makeText(activity, R.string.saving_to_file, Toast.LENGTH_SHORT);
|
||||
new AsyncTask<Context, Void, String>() {
|
||||
@Override
|
||||
protected void onPreExecute() {
|
||||
super.onPreExecute();
|
||||
toast.show();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String doInBackground(Context... params) {
|
||||
try {
|
||||
return String.format(params[0].getString(R.string.saved_playlist_to), PlaylistsUtil.savePlaylist(params[0], playlist));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return String.format(params[0].getString(R.string.failed_to_save_playlist), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(String string) {
|
||||
super.onPostExecute(string);
|
||||
if (toast != null) {
|
||||
toast.setText(string);
|
||||
toast.show();
|
||||
}
|
||||
}
|
||||
}.execute(activity.getApplicationContext());
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue