From 49055daf4b321ea91cd09964495f9d9397dfea1c Mon Sep 17 00:00:00 2001 From: Nathan Cannon Date: Sat, 3 Dec 2016 19:09:11 +0000 Subject: [PATCH] Refined playlist loading: - Removed intermediary collection. - Filtered non-path metadata lines. --- .../musicplayer/model/ExtractedMetadata.java | 3 +- .../java/musicplayer/playlist/IPlaylist.java | 33 ++++++++++++++----- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/main/java/musicplayer/model/ExtractedMetadata.java b/src/main/java/musicplayer/model/ExtractedMetadata.java index cbe5474..4da61d9 100644 --- a/src/main/java/musicplayer/model/ExtractedMetadata.java +++ b/src/main/java/musicplayer/model/ExtractedMetadata.java @@ -77,7 +77,8 @@ public class ExtractedMetadata { Tag audioTags = null; try { audioTags = AudioFileIO.read(targetFile.toFile()).getTag(); - } catch (CannotReadException | IOException | ReadOnlyFileException | TagException | InvalidAudioFrameException ignored) { + } catch (CannotReadException | IOException | ReadOnlyFileException | TagException | + InvalidAudioFrameException | NullPointerException ignored) { } return audioTags == null ? Optional.empty() : Optional.of(new ExtractedMetadata(audioTags, targetFile.toFile())); } diff --git a/src/main/java/musicplayer/playlist/IPlaylist.java b/src/main/java/musicplayer/playlist/IPlaylist.java index b703bb0..66ee0e8 100644 --- a/src/main/java/musicplayer/playlist/IPlaylist.java +++ b/src/main/java/musicplayer/playlist/IPlaylist.java @@ -131,25 +131,40 @@ public interface IPlaylist { * @param targetFile File to read m3u playlist data from. * @return List of songs contained in the playlist. */ - static List readPlaylistFromFile(File targetFile){ + static List readPlaylistFromFile(final 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(ExtractedMetadata::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()))); + playlistData.stream().filter(x -> !x.startsWith("#")).map(Paths::get).map(x -> checkSongPath(x, targetFile)).map(ExtractedMetadata::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; } + /** + * Check whether a Path points to a valid file, if not try again under the assumption that the Path is instead + * relative instead of absolute. If neither then null is returned. + * @param targetFile Path to check if points to a valid file. + * @param playlistFile Playlist file being read (used to potentially transform paths to relative paths). + * @return A Path to a valid file or null. + */ + static Path checkSongPath(final Path targetFile, final File playlistFile){ + if(Files.exists(targetFile)){ + return targetFile; + } + else{ + Path alternateFile = Paths.get(playlistFile.getParent(), targetFile.toString()); + if(Files.exists(alternateFile)) + return alternateFile; + } + return null; + } + }