diff --git a/src/main/java/musicplayer/Player.java b/src/main/java/musicplayer/Player.java index abc6155..123086f 100644 --- a/src/main/java/musicplayer/Player.java +++ b/src/main/java/musicplayer/Player.java @@ -8,6 +8,7 @@ import org.gstreamer.State; import org.gstreamer.elements.PlayBin2; import java.io.File; +import java.util.Optional; public class Player { @@ -43,24 +44,31 @@ public class Player { /** * Set a source Song file and start playing it via GStreamer. - * @param song Song to play. + * @param inputSong Song to play. */ - public void playSong(Song song) { - callbackInterface.setSongHighlighting(song); - currentSong = song; - File songFile = song.getSongFile(); + public void playSong(Optional inputSong){ if (playBin.getState() == State.PLAYING) stop(); playBin.setState(State.READY); - playBin.setURI(songFile.toURI()); - internalThread = new InternalThread(); - thread = new Thread(internalThread); - playBin.play(); - thread.start(); - while(true){ - if (playBin.isPlaying()) break; - } // Wait for song to actually be playing otherwise queryDuration is always zero - callbackInterface.setSeekBarDuration((int) playBin.queryDuration().toSeconds()); + if(inputSong.isPresent()) { + currentSong = inputSong.get(); + callbackInterface.setSongHighlighting(currentSong); + File songFile = currentSong.getSongFile(); + if (!songFile.exists()) { + callbackInterface.removeInvalidSong(currentSong); + playSong(callbackInterface.getNextSong(currentSong)); + return; + } + playBin.setURI(songFile.toURI()); + internalThread = new InternalThread(); + thread = new Thread(internalThread); + playBin.play(); + thread.start(); + while (true) { + if (playBin.isPlaying()) break; + } // Wait for song to actually be playing otherwise queryDuration is always zero + callbackInterface.setSeekBarDuration((int) playBin.queryDuration().toSeconds()); + } } /** diff --git a/src/main/java/musicplayer/PlayerGUI.java b/src/main/java/musicplayer/PlayerGUI.java index edd931b..2a627c9 100644 --- a/src/main/java/musicplayer/PlayerGUI.java +++ b/src/main/java/musicplayer/PlayerGUI.java @@ -66,7 +66,7 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf //Action Listeners playButton.addActionListener(e -> { if (playList.getRowCount() > 0) { - player.playSong(playlistTableModel.getFirst().get()); + player.playSong(playlistTableModel.getFirst()); } }); MouseListener mouseListener = new libraryMouseAdapter(); @@ -150,7 +150,7 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf * @return The next song to be selected. */ @Override - public Song getNextSong(Song currentSong) { + public Optional getNextSong(Song currentSong) { return playlistTableModel.nextSong(currentSong); } @@ -180,6 +180,11 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf SwingUtilities.invokeLater(() -> seekBar.setMaximum(seconds)); } + @Override + public void removeInvalidSong(Song invalidSong) { + playlistTableModel.removeSong(invalidSong); + } + public void refreshLibrary(){ DefaultTreeModel model = new DefaultTreeModel(new DefaultMutableTreeNode()); DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) model.getRoot(); @@ -261,4 +266,5 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf private void setVolumeValue(int value){ volumeValue.setText(String.format("%d%%", value)); } + } diff --git a/src/main/java/musicplayer/PlaylistTableModel.java b/src/main/java/musicplayer/PlaylistTableModel.java index 806e2fe..b9cfe09 100644 --- a/src/main/java/musicplayer/PlaylistTableModel.java +++ b/src/main/java/musicplayer/PlaylistTableModel.java @@ -66,10 +66,10 @@ public class PlaylistTableModel extends AbstractTableModel { return songList.size() > 0 ? Optional.of(songList.get(0)) : Optional.empty(); } - public Song nextSong(Song song){ + public Optional nextSong(Song song){ int index = songList.indexOf(song) + 1; if(index >= songList.size()) index = 0; - return songList.get(index); + return songList.size() > 0 ? Optional.of(songList.get(index)) : Optional.empty(); } public Song previous(Song song){ @@ -94,4 +94,9 @@ public class PlaylistTableModel extends AbstractTableModel { Collections.reverse(indices); indices.forEach(this::removeSong); } + + public void removeSong(Song song){ + songList.remove(song); + fireTableDataChanged(); + } } diff --git a/src/main/java/musicplayer/callbacks/PlayerCallbackInterface.java b/src/main/java/musicplayer/callbacks/PlayerCallbackInterface.java index a63b5aa..45aaa9b 100644 --- a/src/main/java/musicplayer/callbacks/PlayerCallbackInterface.java +++ b/src/main/java/musicplayer/callbacks/PlayerCallbackInterface.java @@ -2,12 +2,16 @@ package musicplayer.callbacks; import musicplayer.model.Song; +import java.util.Optional; + public interface PlayerCallbackInterface { - Song getNextSong(Song currentSong); + Optional getNextSong(Song currentSong); void setSongHighlighting(Song playingSong); void setSeekBarDuration(int seconds); + void removeInvalidSong(Song invalidSong); + }