diff --git a/src/main/java/musicplayer/PlayerGUI.java b/src/main/java/musicplayer/PlayerGUI.java index 6f61e1c..3869875 100644 --- a/src/main/java/musicplayer/PlayerGUI.java +++ b/src/main/java/musicplayer/PlayerGUI.java @@ -9,7 +9,6 @@ import musicplayer.player.IPlayer; import musicplayer.playlist.IPlaylist; import musicplayer.util.ConfigManager; import musicplayer.util.Icons; -import musicplayer.util.PlaylistUtils; import org.jnativehook.GlobalScreen; import org.jnativehook.NativeHookException; import org.jnativehook.keyboard.NativeKeyEvent; @@ -263,7 +262,7 @@ public class PlayerGUI extends JPanel implements PlayerCallbackInterface { if (!filename.endsWith(".m3u")) filename += ".m3u"; try { - PlaylistUtils.writePlaylistToFile(playlist.getSongList(), new File(filename)); + IPlaylist.writePlaylistToFile(playlist.getSongList(), new File(filename)); } catch (IOException e1) { JOptionPane.showMessageDialog(null, e1.getMessage() + "\n" + Arrays.toString(e1.getStackTrace()), "Error", JOptionPane.ERROR_MESSAGE); } @@ -278,7 +277,7 @@ public class PlayerGUI extends JPanel implements PlayerCallbackInterface { fileChooser.setDialogType(JFileChooser.OPEN_DIALOG); fileChooser.setFileFilter(m3uExtensionFilter); if(fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION){ - PlaylistUtils.readPlaylistFromFile(fileChooser.getSelectedFile()).stream().forEach(playlist::addSong); + IPlaylist.readPlaylistFromFile(fileChooser.getSelectedFile()).stream().forEach(playlist::addSong); } }); playlistTools.add(menuItem); diff --git a/src/main/java/musicplayer/playlist/IPlaylist.java b/src/main/java/musicplayer/playlist/IPlaylist.java index c41947e..40573df 100644 --- a/src/main/java/musicplayer/playlist/IPlaylist.java +++ b/src/main/java/musicplayer/playlist/IPlaylist.java @@ -1,9 +1,18 @@ package musicplayer.playlist; +import musicplayer.library.ILibrary; +import musicplayer.model.Album; +import musicplayer.model.Artist; import musicplayer.model.Song; +import java.io.*; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; import java.util.List; import java.util.Optional; +import java.util.stream.Collectors; public interface IPlaylist { void addSong(Song song); @@ -22,4 +31,48 @@ public interface IPlaylist { void setPlayingSong(Song song); void setStopped(); + /** + * Write a list of songs to a file as an m3u format playlist file. + * @param playlist Playlist of songs to write. + * @param targetFile File to write playlist into. + * @throws IOException + */ + static void writePlaylistToFile(List playlist, File targetFile) throws IOException { + if(!targetFile.exists()) + targetFile.createNewFile(); + try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(targetFile), "utf-8"))) { + writer.write("#EXTM3U\n"); + for(Song song : playlist){ + writer.write(song.getM3UFormatString()); + } + } + } + + /** + * Read songs in from an m3u playlist. + * Songs in the playlist with files that don't actually exists will be dropped. + * @param targetFile File to read m3u playlist data from. + * @return List of songs contained in the playlist. + */ + static List readPlaylistFromFile(File targetFile){ + List result = new ArrayList<>(); + if(targetFile.exists()){ + try { + List playlistData = Files.lines(targetFile.toPath()).filter(x -> !x.isEmpty() && !x.contains("#EXTINF")).collect(Collectors.toList()); + if(playlistData.get(0).equals("#EXTM3U")){ + playlistData.remove(0); + List songPaths = playlistData.stream().map(Paths::get).collect(Collectors.toList()); + for(int i = 0; i < songPaths.size(); i++){ + if(!Files.exists(songPaths.get(i))){ // If song file doesn't exists, try treating it as relative + songPaths.set(i, Paths.get(targetFile.getParent(), songPaths.get(i).toString())); + } + } + songPaths.stream().map(ILibrary::autoParse).filter(Optional::isPresent).map(Optional::get) + .forEach(x -> result.add(new Song(x.getTrackNumber(), x.getDiscNumber(), x.getTitle(), new Artist(x.getArtist()), new Album(x.getAlbum()), x.getGenre(), x.getSongFile()))); + } + } catch (IOException ignored) { } + } + return result; + } + } diff --git a/src/main/java/musicplayer/util/PlaylistUtils.java b/src/main/java/musicplayer/util/PlaylistUtils.java deleted file mode 100644 index 95c7a42..0000000 --- a/src/main/java/musicplayer/util/PlaylistUtils.java +++ /dev/null @@ -1,62 +0,0 @@ -package musicplayer.util; - -import musicplayer.library.ILibrary; -import musicplayer.model.Album; -import musicplayer.model.Artist; -import musicplayer.model.Song; - -import java.io.*; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.ArrayList; -import java.util.List; -import java.util.Optional; -import java.util.stream.Collectors; - -public final class PlaylistUtils { - - /** - * Write a list of songs to a file as an m3u format playlist file. - * @param playlist Playlist of songs to write. - * @param targetFile File to write playlist into. - * @throws IOException - */ - public static void writePlaylistToFile(List playlist, File targetFile) throws IOException { - if(!targetFile.exists()) - targetFile.createNewFile(); - try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(targetFile), "utf-8"))) { - writer.write("#EXTM3U\n"); - for(Song song : playlist){ - writer.write(song.getM3UFormatString()); - } - } - } - - /** - * Read songs in from an m3u playlist. - * Songs in the playlist with files that don't actually exists will be dropped. - * @param targetFile File to read m3u playlist data from. - * @return List of songs contained in the playlist. - */ - public static List readPlaylistFromFile(File targetFile){ - List result = new ArrayList<>(); - if(targetFile.exists()){ - try { - List playlistData = Files.lines(targetFile.toPath()).filter(x -> !x.isEmpty() && !x.contains("#EXTINF")).collect(Collectors.toList()); - if(playlistData.get(0).equals("#EXTM3U")){ - playlistData.remove(0); - List songPaths = playlistData.stream().map(Paths::get).collect(Collectors.toList()); - for(int i = 0; i < songPaths.size(); i++){ - if(!Files.exists(songPaths.get(i))){ // If song file doesn't exists, try treating it as relative - songPaths.set(i, Paths.get(targetFile.getParent(), songPaths.get(i).toString())); - } - } - songPaths.stream().map(ILibrary::autoParse).filter(Optional::isPresent).map(Optional::get) - .forEach(x -> result.add(new Song(x.getTrackNumber(), x.getDiscNumber(), x.getTitle(), new Artist(x.getArtist()), new Album(x.getAlbum()), x.getGenre(), x.getSongFile()))); - } - } catch (IOException ignored) { } - } - return result; - } -} diff --git a/src/test/java/musicplayer/playlist/IPlaylistTest.java b/src/test/java/musicplayer/playlist/IPlaylistTest.java new file mode 100644 index 0000000..783dc9e --- /dev/null +++ b/src/test/java/musicplayer/playlist/IPlaylistTest.java @@ -0,0 +1,31 @@ +package musicplayer.playlist; + +import musicplayer.library.ILibrary; +import musicplayer.model.Song; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.*; + +public class IPlaylistTest { + + @Rule + public TemporaryFolder folder = new TemporaryFolder(); + + @Test + public void testWriteAndReadPlaylist() throws Exception { + Song sampleSong = new Song(ILibrary.autoParse( + new File(IPlaylistTest.class.getResource("/sample.mp3").getFile()).toPath()).get()); + File playlistFile = folder.newFile(); + List playlist = new ArrayList<>(); + playlist.add(sampleSong); + IPlaylist.writePlaylistToFile(playlist, playlistFile); + List result = IPlaylist.readPlaylistFromFile(playlistFile); + assertEquals(sampleSong, result.get(0)); + } +} \ No newline at end of file