diff --git a/src/main/java/musicplayer/Player.java b/src/main/java/musicplayer/Player.java index bf13311..e4b44b0 100644 --- a/src/main/java/musicplayer/Player.java +++ b/src/main/java/musicplayer/Player.java @@ -26,7 +26,7 @@ public class Player { playBin.connect((PlayBin2.ABOUT_TO_FINISH) playBin2 -> { Thread callbackThing = new Thread(() -> { try { - Thread.sleep(1500); // Song is about to finish, wait so we don't cut it off early. + Thread.sleep(1000); // Song is about to finish, wait so we don't cut it off early. } catch (InterruptedException e) { e.printStackTrace(); } @@ -42,18 +42,22 @@ public class Player { File songFile = song.getSongFile(); 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()); } public void stop() { if (this.thread != null) { playBin.stop(); internalThread.stop(); - thread.interrupt(); thread = null; } } @@ -70,6 +74,10 @@ public class Player { return currentSong; } + public int currentSongPosition(){ + return playBin.isPlaying() ? (int) (playBin.getClock().getTime().toSeconds() - playBin.getBaseTime().toSeconds()) : 0; + } + private class InternalThread implements Runnable { @Override diff --git a/src/main/java/musicplayer/PlayerCallbackInterface.java b/src/main/java/musicplayer/PlayerCallbackInterface.java index 28c7da4..2e76fda 100644 --- a/src/main/java/musicplayer/PlayerCallbackInterface.java +++ b/src/main/java/musicplayer/PlayerCallbackInterface.java @@ -7,4 +7,7 @@ public interface PlayerCallbackInterface { Song getNextSong(Song currentSong); void setSongHighlighting(Song playingSong); + + void setSeekBarDuration(int seconds); + } diff --git a/src/main/java/musicplayer/PlayerGUI.form b/src/main/java/musicplayer/PlayerGUI.form index 1909841..3240931 100644 --- a/src/main/java/musicplayer/PlayerGUI.form +++ b/src/main/java/musicplayer/PlayerGUI.form @@ -1,6 +1,6 @@
- + @@ -10,7 +10,7 @@ - + @@ -38,7 +38,7 @@ - + @@ -67,6 +67,19 @@ + + + + + + + + + + + + + diff --git a/src/main/java/musicplayer/PlayerGUI.java b/src/main/java/musicplayer/PlayerGUI.java index b250ee3..4700468 100644 --- a/src/main/java/musicplayer/PlayerGUI.java +++ b/src/main/java/musicplayer/PlayerGUI.java @@ -19,6 +19,7 @@ public class PlayerGUI implements PlayerCallbackInterface{ private JButton playButton; private JButton stopButton; private JButton nextButton; + private JSlider seekBar; private Player player = new Player(this); private PlaylistTableModel playlistTableModel = new PlaylistTableModel(new ArrayList<>()); @@ -50,6 +51,23 @@ public class PlayerGUI implements PlayerCallbackInterface{ }); stopButton.addActionListener(e -> player.stop()); nextButton.addActionListener(e -> playNextSong()); + + Thread seekBarUpdater = new Thread(() -> { + boolean running = true; + while (running) { + try { + Thread.sleep(1000); + setSeekBarPosition(player.currentSongPosition()); + } catch (InterruptedException e) { + running = false; + } + } + }); + seekBarUpdater.start(); + } + + public void setSeekBarPosition(int position){ + SwingUtilities.invokeLater(() -> seekBar.setValue(position)); } private void resetTree() { @@ -101,9 +119,14 @@ public class PlayerGUI implements PlayerCallbackInterface{ } public void playNextSong(){ + SwingUtilities.invokeLater(() -> seekBar.setValue(0)); player.playSong(getNextSong(player.getCurrentSong())); } + public void setSeekBarDuration(int seconds){ + SwingUtilities.invokeLater(() -> seekBar.setMaximum(seconds)); + } + private class libraryMouseAdapter extends MouseAdapter { @Override public void mousePressed(MouseEvent e) { diff --git a/src/main/java/musicplayer/PlaylistTableModel.java b/src/main/java/musicplayer/PlaylistTableModel.java index 9495ec4..359d177 100644 --- a/src/main/java/musicplayer/PlaylistTableModel.java +++ b/src/main/java/musicplayer/PlaylistTableModel.java @@ -70,6 +70,12 @@ public class PlaylistTableModel extends AbstractTableModel { return songList.get(index); } + public Song previous(Song song){ + int index = songList.indexOf(song) - 1; + if(index < 0) index = songList.size() - 1; + return songList.get(index); + } + public int getSongIndex(Song song){ return songList.indexOf(song); }