Skip to content

Commit

Permalink
Update GUI to reflect changes and add seeking.
Browse files Browse the repository at this point in the history
  • Loading branch information
fxb committed Apr 12, 2010
1 parent f5fa070 commit 7291b8d
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/de/felixbruns/jotify/gui/JotifyApplication.java
Expand Up @@ -87,6 +87,7 @@ public void initialize(){

/* Create player. */
this.player = new JotifyPlayer(this.jotify);

this.broadcast.addControlListener(this.player);

/* Create scrobbler if enabled. */
Expand Down
28 changes: 22 additions & 6 deletions src/de/felixbruns/jotify/gui/JotifyPlayer.java
@@ -1,15 +1,16 @@
package de.felixbruns.jotify.gui;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeoutException;

import de.felixbruns.jotify.Jotify;
import de.felixbruns.jotify.exceptions.AuthenticationException;
import de.felixbruns.jotify.exceptions.ConnectionException;
import de.felixbruns.jotify.gui.listeners.ControlListener;
import de.felixbruns.jotify.gui.listeners.JotifyBroadcast;
import de.felixbruns.jotify.gui.listeners.PlayerListener.Status;
import de.felixbruns.jotify.media.File;
import de.felixbruns.jotify.media.Track;
import de.felixbruns.jotify.player.PlaybackListener;

Expand All @@ -22,6 +23,7 @@ public class JotifyPlayer implements ControlListener, PlaybackListener {
private JotifyBroadcast broadcast;
private JotifyPlaybackQueue queue;
private List<PlaybackListener> listeners;
private float volume;

private final Jotify jotify;

Expand All @@ -30,6 +32,7 @@ public JotifyPlayer(final Jotify jotify) throws ConnectionException, Authenticat
this.queue = new JotifyPlaybackQueue();
this.listeners = new ArrayList<PlaybackListener>();
this.jotify = jotify;
this.volume = 1.0f;
}

public void addPlaybackListener(PlaybackListener listener){
Expand Down Expand Up @@ -60,12 +63,13 @@ public void controlPrevious(){
this.jotify.stop();

try{
this.jotify.play(track, this);
this.jotify.play(track, File.BITRATE_160, this);
this.jotify.volume(this.volume);

this.broadcast.firePlayerTrackChanged(track);
this.broadcast.fireQueueUpdated(this.queue);
}
catch(TimeoutException e){
catch(Exception e){
e.printStackTrace();
}
}
Expand All @@ -78,22 +82,34 @@ public void controlNext(){
this.jotify.stop();

try{
this.jotify.play(track, this);
this.jotify.play(track, File.BITRATE_160, this);
this.jotify.volume(this.volume);

this.broadcast.firePlayerTrackChanged(track);
this.broadcast.firePlayerStatusChanged(Status.PLAY);
this.broadcast.fireQueueUpdated(this.queue);
}
catch(TimeoutException e){
catch(Exception e){
e.printStackTrace();
}
}
}

public void controlVolume(float volume) {
public void controlVolume(float volume){
this.volume = volume;

this.jotify.volume(volume);
}

public void controlSeek(float percent){
try{
this.jotify.seek((int)(percent * this.jotify.length()));
}
catch(IOException e){
e.printStackTrace();
}
}

public void controlSelect(Track track){
if(!this.queue.hasCurrent()){
this.queue.add(track);
Expand Down
Expand Up @@ -10,6 +10,7 @@ public interface ControlListener {
public void controlPrevious();
public void controlNext();
public void controlVolume(float volume);
public void controlSeek(float percent);
public void controlSelect(Track track);
public void controlSelect(List<Track> tracks);
public void controlQueue(Track track);
Expand Down
6 changes: 6 additions & 0 deletions src/de/felixbruns/jotify/gui/listeners/JotifyBroadcast.java
Expand Up @@ -166,6 +166,12 @@ public void fireControlVolume(float volume){
}
}

public void fireControlSeek(float percent){
for(ControlListener listener : this.controlListeners){
listener.controlSeek(percent);
}
}

public void fireControlSelect(Track track){
for(ControlListener listener : this.controlListeners){
listener.controlSelect(track);
Expand Down
Expand Up @@ -49,6 +49,8 @@ public class JotifyControlPanel extends JPanel implements QueueListener, PlayerL
private JSeparator separator;
private Track track;

private boolean wasAdjusting = false;

public JotifyControlPanel(){
this.broadcast = JotifyBroadcast.getInstance();

Expand Down Expand Up @@ -161,6 +163,15 @@ public void stateChanged(ChangeEvent e){
this.positionSlider.setMinimum(0);
this.positionSlider.setMaximum(1000);
this.positionSlider.setValue(0);
this.positionSlider.addChangeListener(new ChangeListener(){
public void stateChanged(ChangeEvent e){
if(wasAdjusting && !positionSlider.getValueIsAdjusting()){
JotifyBroadcast.getInstance().fireControlSeek(positionSlider.getValue() / 1000.0f);
}

wasAdjusting = positionSlider.getValueIsAdjusting();
}
});
this.add(this.positionSlider, BorderLayout.CENTER);

/* Add remaining label. */
Expand Down Expand Up @@ -216,7 +227,7 @@ public void playerStatusChanged(Status status){

public void playerPositionChanged(int ms){
/* Return if nothing is playing. */
if(this.track == null){
if(this.track == null || this.positionSlider.getValueIsAdjusting()){
return;
}

Expand All @@ -229,6 +240,8 @@ public void playerPositionChanged(int ms){
/* Update labels and slider. */
this.positionLabel.setText(TimeFormatter.formatSeconds(position));
this.remainingLabel.setText(TimeFormatter.formatRemainingSeconds(remaining));

/* Update slider. */
this.positionSlider.setValue(progress);
}
}

0 comments on commit 7291b8d

Please sign in to comment.