Added seek bar that auto updates with music play duration. Currently no click seek support.

This commit is contained in:
neviyn 2016-02-11 18:07:41 +00:00
parent de860fad1f
commit fe8c1adb39
5 changed files with 58 additions and 5 deletions

View File

@ -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

View File

@ -7,4 +7,7 @@ public interface PlayerCallbackInterface {
Song getNextSong(Song currentSong);
void setSongHighlighting(Song playingSong);
void setSeekBarDuration(int seconds);
}

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="musicplayer.PlayerGUI">
<grid id="27dc6" binding="mainPanel" layout-manager="GridLayoutManager" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="27dc6" binding="mainPanel" layout-manager="GridLayoutManager" row-count="3" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="500" height="400"/>
@ -10,7 +10,7 @@
<children>
<scrollpane id="2b209">
<constraints>
<grid row="0" column="0" row-span="2" col-span="1" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
<grid row="0" column="0" row-span="3" col-span="1" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
@ -38,7 +38,7 @@
</scrollpane>
<toolbar id="56df">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false">
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="-1" height="20"/>
</grid>
</constraints>
@ -67,6 +67,19 @@
</component>
</children>
</toolbar>
<component id="98c55" class="javax.swing.JSlider" binding="seekBar">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<inverted value="false"/>
<minorTickSpacing value="1"/>
<paintTicks value="false"/>
<snapToTicks value="false"/>
<value value="0"/>
<valueIsAdjusting value="false"/>
</properties>
</component>
</children>
</grid>
</form>

View File

@ -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) {

View File

@ -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);
}