Added seek bar that auto updates with music play duration. Currently no click seek support.
This commit is contained in:
parent
de860fad1f
commit
fe8c1adb39
@ -26,7 +26,7 @@ public class Player {
|
|||||||
playBin.connect((PlayBin2.ABOUT_TO_FINISH) playBin2 -> {
|
playBin.connect((PlayBin2.ABOUT_TO_FINISH) playBin2 -> {
|
||||||
Thread callbackThing = new Thread(() -> {
|
Thread callbackThing = new Thread(() -> {
|
||||||
try {
|
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) {
|
} catch (InterruptedException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
@ -42,18 +42,22 @@ public class Player {
|
|||||||
File songFile = song.getSongFile();
|
File songFile = song.getSongFile();
|
||||||
if (playBin.getState() == State.PLAYING)
|
if (playBin.getState() == State.PLAYING)
|
||||||
stop();
|
stop();
|
||||||
|
playBin.setState(State.READY);
|
||||||
playBin.setURI(songFile.toURI());
|
playBin.setURI(songFile.toURI());
|
||||||
internalThread = new InternalThread();
|
internalThread = new InternalThread();
|
||||||
thread = new Thread(internalThread);
|
thread = new Thread(internalThread);
|
||||||
playBin.play();
|
playBin.play();
|
||||||
thread.start();
|
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() {
|
public void stop() {
|
||||||
if (this.thread != null) {
|
if (this.thread != null) {
|
||||||
playBin.stop();
|
playBin.stop();
|
||||||
internalThread.stop();
|
internalThread.stop();
|
||||||
thread.interrupt();
|
|
||||||
thread = null;
|
thread = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -70,6 +74,10 @@ public class Player {
|
|||||||
return currentSong;
|
return currentSong;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int currentSongPosition(){
|
||||||
|
return playBin.isPlaying() ? (int) (playBin.getClock().getTime().toSeconds() - playBin.getBaseTime().toSeconds()) : 0;
|
||||||
|
}
|
||||||
|
|
||||||
private class InternalThread implements Runnable {
|
private class InternalThread implements Runnable {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -7,4 +7,7 @@ public interface PlayerCallbackInterface {
|
|||||||
Song getNextSong(Song currentSong);
|
Song getNextSong(Song currentSong);
|
||||||
|
|
||||||
void setSongHighlighting(Song playingSong);
|
void setSongHighlighting(Song playingSong);
|
||||||
|
|
||||||
|
void setSeekBarDuration(int seconds);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="musicplayer.PlayerGUI">
|
<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"/>
|
<margin top="0" left="0" bottom="0" right="0"/>
|
||||||
<constraints>
|
<constraints>
|
||||||
<xy x="20" y="20" width="500" height="400"/>
|
<xy x="20" y="20" width="500" height="400"/>
|
||||||
@ -10,7 +10,7 @@
|
|||||||
<children>
|
<children>
|
||||||
<scrollpane id="2b209">
|
<scrollpane id="2b209">
|
||||||
<constraints>
|
<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>
|
</constraints>
|
||||||
<properties/>
|
<properties/>
|
||||||
<border type="none"/>
|
<border type="none"/>
|
||||||
@ -38,7 +38,7 @@
|
|||||||
</scrollpane>
|
</scrollpane>
|
||||||
<toolbar id="56df">
|
<toolbar id="56df">
|
||||||
<constraints>
|
<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"/>
|
<preferred-size width="-1" height="20"/>
|
||||||
</grid>
|
</grid>
|
||||||
</constraints>
|
</constraints>
|
||||||
@ -67,6 +67,19 @@
|
|||||||
</component>
|
</component>
|
||||||
</children>
|
</children>
|
||||||
</toolbar>
|
</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>
|
</children>
|
||||||
</grid>
|
</grid>
|
||||||
</form>
|
</form>
|
||||||
|
@ -19,6 +19,7 @@ public class PlayerGUI implements PlayerCallbackInterface{
|
|||||||
private JButton playButton;
|
private JButton playButton;
|
||||||
private JButton stopButton;
|
private JButton stopButton;
|
||||||
private JButton nextButton;
|
private JButton nextButton;
|
||||||
|
private JSlider seekBar;
|
||||||
private Player player = new Player(this);
|
private Player player = new Player(this);
|
||||||
private PlaylistTableModel playlistTableModel = new PlaylistTableModel(new ArrayList<>());
|
private PlaylistTableModel playlistTableModel = new PlaylistTableModel(new ArrayList<>());
|
||||||
|
|
||||||
@ -50,6 +51,23 @@ public class PlayerGUI implements PlayerCallbackInterface{
|
|||||||
});
|
});
|
||||||
stopButton.addActionListener(e -> player.stop());
|
stopButton.addActionListener(e -> player.stop());
|
||||||
nextButton.addActionListener(e -> playNextSong());
|
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() {
|
private void resetTree() {
|
||||||
@ -101,9 +119,14 @@ public class PlayerGUI implements PlayerCallbackInterface{
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void playNextSong(){
|
public void playNextSong(){
|
||||||
|
SwingUtilities.invokeLater(() -> seekBar.setValue(0));
|
||||||
player.playSong(getNextSong(player.getCurrentSong()));
|
player.playSong(getNextSong(player.getCurrentSong()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setSeekBarDuration(int seconds){
|
||||||
|
SwingUtilities.invokeLater(() -> seekBar.setMaximum(seconds));
|
||||||
|
}
|
||||||
|
|
||||||
private class libraryMouseAdapter extends MouseAdapter {
|
private class libraryMouseAdapter extends MouseAdapter {
|
||||||
@Override
|
@Override
|
||||||
public void mousePressed(MouseEvent e) {
|
public void mousePressed(MouseEvent e) {
|
||||||
|
@ -70,6 +70,12 @@ public class PlaylistTableModel extends AbstractTableModel {
|
|||||||
return songList.get(index);
|
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){
|
public int getSongIndex(Song song){
|
||||||
return songList.indexOf(song);
|
return songList.indexOf(song);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user