Added additional playback controls.

This commit is contained in:
neviyn 2016-02-26 21:44:35 +00:00
parent 069e75b4cd
commit 35c953f473
3 changed files with 27 additions and 15 deletions

View File

@ -51,8 +51,12 @@ public class Player {
public void playSong(Optional<Song> inputSong){ public void playSong(Optional<Song> inputSong){
if (playBin.getState() == State.PLAYING) if (playBin.getState() == State.PLAYING)
stop(); stop();
playBin.setState(State.READY);
if(inputSong.isPresent()) { if(inputSong.isPresent()) {
if(playBin.getState() == State.PAUSED && inputSong.get() == currentSong) {
resume();
return;
}
playBin.setState(State.READY);
currentSong = inputSong.get(); currentSong = inputSong.get();
callbackInterface.setSongHighlighting(currentSong); callbackInterface.setSongHighlighting(currentSong);
File songFile = currentSong.getSongFile(); File songFile = currentSong.getSongFile();
@ -111,7 +115,7 @@ public class Player {
* @return Current playback position in seconds. * @return Current playback position in seconds.
*/ */
public int currentSongPosition(){ 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;
} }
/** /**

View File

@ -156,6 +156,11 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf
player.playSong(playlistTableModel.nextSong(player.getCurrentSong())); 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. * Set the maximum value of the seekBar.
* *
@ -285,8 +290,8 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf
playList.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); playList.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
playList.getColumnModel().getColumn(0).setPreferredWidth(10); playList.getColumnModel().getColumn(0).setPreferredWidth(10);
playList.getColumnModel().getColumn(0).setMaxWidth(10); playList.getColumnModel().getColumn(0).setMaxWidth(10);
playList.getColumnModel().getColumn(1).setPreferredWidth(30); playList.getColumnModel().getColumn(1).setPreferredWidth(40);
playList.getColumnModel().getColumn(1).setMaxWidth(30); playList.getColumnModel().getColumn(1).setMaxWidth(40);
JPopupMenu popupMenu = new JPopupMenu(); JPopupMenu popupMenu = new JPopupMenu();
JMenuItem menuItem = new JMenuItem("Remove"); JMenuItem menuItem = new JMenuItem("Remove");
menuItem.addActionListener((e) -> playlistTableModel.removeSong(playList.getSelectedRows())); menuItem.addActionListener((e) -> playlistTableModel.removeSong(playList.getSelectedRows()));
@ -304,8 +309,7 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf
private JToolBar createControlButtons() { private JToolBar createControlButtons() {
JToolBar toolBar = new JToolBar(); JToolBar toolBar = new JToolBar();
toolBar.setFloatable(false); toolBar.setFloatable(false);
JButton playButton = new JButton(); JButton playButton = new JButton("Play");
playButton.setText("Play");
playButton.setMnemonic('P'); playButton.setMnemonic('P');
playButton.setDisplayedMnemonicIndex(0); playButton.setDisplayedMnemonicIndex(0);
playButton.addActionListener(e -> { playButton.addActionListener(e -> {
@ -317,14 +321,20 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf
} }
}); });
toolBar.add(playButton); toolBar.add(playButton);
JButton stopButton = new JButton(); JButton pauseButton = new JButton("Pause");
stopButton.setText("Stop"); pauseButton.setMnemonic('E');
pauseButton.addActionListener(e -> player.pause());
toolBar.add(pauseButton);
JButton stopButton = new JButton("Stop");
stopButton.setMnemonic('S'); stopButton.setMnemonic('S');
stopButton.setDisplayedMnemonicIndex(0); stopButton.setDisplayedMnemonicIndex(0);
stopButton.addActionListener(e -> player.stop()); stopButton.addActionListener(e -> player.stop());
toolBar.add(stopButton); toolBar.add(stopButton);
JButton nextButton = new JButton(); JButton previousButton = new JButton("Previous");
nextButton.setText("Next"); previousButton.setMnemonic('R');
previousButton.addActionListener(e -> playPreviousSong());
toolBar.add(previousButton);
JButton nextButton = new JButton("Next");
nextButton.setMnemonic('N'); nextButton.setMnemonic('N');
nextButton.setDisplayedMnemonicIndex(0); nextButton.setDisplayedMnemonicIndex(0);
nextButton.addActionListener(e -> playNextSong()); nextButton.addActionListener(e -> playNextSong());

View File

@ -23,7 +23,7 @@ public class PlaylistTableModel extends AbstractTableModel {
@Override @Override
public int getColumnCount() { public int getColumnCount() {
return 6; return 5;
} }
@Override @Override
@ -36,7 +36,6 @@ public class PlaylistTableModel extends AbstractTableModel {
case 2: o = song.getTitle(); break; case 2: o = song.getTitle(); break;
case 3: o = song.getArtist(); break; case 3: o = song.getArtist(); break;
case 4: o = song.getAlbum(); break; case 4: o = song.getAlbum(); break;
case 5: o = song.getDiscNumber(); break;
} }
return o; return o;
} }
@ -55,7 +54,6 @@ public class PlaylistTableModel extends AbstractTableModel {
case 2: name = "Title"; break; case 2: name = "Title"; break;
case 3: name = "Artist"; break; case 3: name = "Artist"; break;
case 4: name = "Album"; break; case 4: name = "Album"; break;
case 5: name = "DiscNo"; break;
} }
return name; return name;
} }
@ -75,10 +73,10 @@ public class PlaylistTableModel extends AbstractTableModel {
return songList.size() > 0 ? Optional.of(songList.get(index)) : Optional.empty(); return songList.size() > 0 ? Optional.of(songList.get(index)) : Optional.empty();
} }
public Song previous(Song song){ public Optional<Song> previous(Song song){
int index = songList.indexOf(song) - 1; int index = songList.indexOf(song) - 1;
if(index < 0) index = songList.size() - 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){ public int getSongIndex(Song song){