From 35c953f4733a2533f584daa586067c71b2e6b149 Mon Sep 17 00:00:00 2001 From: Nathan Cannon Date: Fri, 26 Feb 2016 21:44:35 +0000 Subject: [PATCH] Added additional playback controls. --- src/main/java/musicplayer/Player.java | 8 ++++-- src/main/java/musicplayer/PlayerGUI.java | 26 +++++++++++++------ .../swingmodels/PlaylistTableModel.java | 8 +++--- 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/src/main/java/musicplayer/Player.java b/src/main/java/musicplayer/Player.java index 173e047..b3ffa98 100644 --- a/src/main/java/musicplayer/Player.java +++ b/src/main/java/musicplayer/Player.java @@ -51,8 +51,12 @@ public class Player { public void playSong(Optional inputSong){ if (playBin.getState() == State.PLAYING) stop(); - playBin.setState(State.READY); if(inputSong.isPresent()) { + if(playBin.getState() == State.PAUSED && inputSong.get() == currentSong) { + resume(); + return; + } + playBin.setState(State.READY); currentSong = inputSong.get(); callbackInterface.setSongHighlighting(currentSong); File songFile = currentSong.getSongFile(); @@ -111,7 +115,7 @@ public class Player { * @return Current playback position in seconds. */ public int currentSongPosition(){ - return playBin.isPlaying() ? (int) (playBin.getClock().getTime().toSeconds() - playBin.getBaseTime().toSeconds()) : 0; + return playBin.isPlaying() || playBin.getState() == State.PAUSED ? (int) (playBin.getClock().getTime().toSeconds() - playBin.getBaseTime().toSeconds()) : 0; } /** diff --git a/src/main/java/musicplayer/PlayerGUI.java b/src/main/java/musicplayer/PlayerGUI.java index ccdec90..c2ff89f 100644 --- a/src/main/java/musicplayer/PlayerGUI.java +++ b/src/main/java/musicplayer/PlayerGUI.java @@ -156,6 +156,11 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf player.playSong(playlistTableModel.nextSong(player.getCurrentSong())); } + public void playPreviousSong() { + SwingUtilities.invokeLater(() -> seekBar.setValue(0)); + player.playSong(playlistTableModel.previous(player.getCurrentSong())); + } + /** * Set the maximum value of the seekBar. * @@ -285,8 +290,8 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf playList.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); playList.getColumnModel().getColumn(0).setPreferredWidth(10); playList.getColumnModel().getColumn(0).setMaxWidth(10); - playList.getColumnModel().getColumn(1).setPreferredWidth(30); - playList.getColumnModel().getColumn(1).setMaxWidth(30); + playList.getColumnModel().getColumn(1).setPreferredWidth(40); + playList.getColumnModel().getColumn(1).setMaxWidth(40); JPopupMenu popupMenu = new JPopupMenu(); JMenuItem menuItem = new JMenuItem("Remove"); menuItem.addActionListener((e) -> playlistTableModel.removeSong(playList.getSelectedRows())); @@ -304,8 +309,7 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf private JToolBar createControlButtons() { JToolBar toolBar = new JToolBar(); toolBar.setFloatable(false); - JButton playButton = new JButton(); - playButton.setText("Play"); + JButton playButton = new JButton("Play"); playButton.setMnemonic('P'); playButton.setDisplayedMnemonicIndex(0); playButton.addActionListener(e -> { @@ -317,14 +321,20 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf } }); toolBar.add(playButton); - JButton stopButton = new JButton(); - stopButton.setText("Stop"); + JButton pauseButton = new JButton("Pause"); + pauseButton.setMnemonic('E'); + pauseButton.addActionListener(e -> player.pause()); + toolBar.add(pauseButton); + JButton stopButton = new JButton("Stop"); stopButton.setMnemonic('S'); stopButton.setDisplayedMnemonicIndex(0); stopButton.addActionListener(e -> player.stop()); toolBar.add(stopButton); - JButton nextButton = new JButton(); - nextButton.setText("Next"); + JButton previousButton = new JButton("Previous"); + previousButton.setMnemonic('R'); + previousButton.addActionListener(e -> playPreviousSong()); + toolBar.add(previousButton); + JButton nextButton = new JButton("Next"); nextButton.setMnemonic('N'); nextButton.setDisplayedMnemonicIndex(0); nextButton.addActionListener(e -> playNextSong()); diff --git a/src/main/java/musicplayer/swingmodels/PlaylistTableModel.java b/src/main/java/musicplayer/swingmodels/PlaylistTableModel.java index e442b7c..41a2f1f 100644 --- a/src/main/java/musicplayer/swingmodels/PlaylistTableModel.java +++ b/src/main/java/musicplayer/swingmodels/PlaylistTableModel.java @@ -23,7 +23,7 @@ public class PlaylistTableModel extends AbstractTableModel { @Override public int getColumnCount() { - return 6; + return 5; } @Override @@ -36,7 +36,6 @@ public class PlaylistTableModel extends AbstractTableModel { case 2: o = song.getTitle(); break; case 3: o = song.getArtist(); break; case 4: o = song.getAlbum(); break; - case 5: o = song.getDiscNumber(); break; } return o; } @@ -55,7 +54,6 @@ public class PlaylistTableModel extends AbstractTableModel { case 2: name = "Title"; break; case 3: name = "Artist"; break; case 4: name = "Album"; break; - case 5: name = "DiscNo"; break; } return name; } @@ -75,10 +73,10 @@ public class PlaylistTableModel extends AbstractTableModel { return songList.size() > 0 ? Optional.of(songList.get(index)) : Optional.empty(); } - public Song previous(Song song){ + public Optional previous(Song song){ int index = songList.indexOf(song) - 1; if(index < 0) index = songList.size() - 1; - return songList.get(index); + return songList.size() > 0 ? Optional.of(songList.get(index)) : Optional.empty(); } public int getSongIndex(Song song){