Implemented pausing, fixed up some fiddly bits.
git-svn-id: https://samskivert.googlecode.com/svn/trunk@1490 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
@@ -15,6 +15,9 @@ import com.samskivert.swing.*;
|
|||||||
import com.samskivert.swing.util.*;
|
import com.samskivert.swing.util.*;
|
||||||
import com.samskivert.util.StringUtil;
|
import com.samskivert.util.StringUtil;
|
||||||
|
|
||||||
|
import com.threerings.presents.dobj.AttributeChangeListener;
|
||||||
|
import com.threerings.presents.dobj.AttributeChangedEvent;
|
||||||
|
|
||||||
import robodj.Log;
|
import robodj.Log;
|
||||||
import robodj.Version;
|
import robodj.Version;
|
||||||
import robodj.repository.*;
|
import robodj.repository.*;
|
||||||
@@ -23,7 +26,7 @@ import robodj.util.FancyPanel;
|
|||||||
import robodj.util.RDJPrefs;
|
import robodj.util.RDJPrefs;
|
||||||
|
|
||||||
public class ChooserFrame extends JFrame
|
public class ChooserFrame extends JFrame
|
||||||
implements ControllerProvider
|
implements ControllerProvider, AttributeChangeListener
|
||||||
{
|
{
|
||||||
public ChooserFrame ()
|
public ChooserFrame ()
|
||||||
{
|
{
|
||||||
@@ -115,8 +118,9 @@ public class ChooserFrame extends JFrame
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// TODO: add ourselves as a playing listener
|
// listen for pause and stop and whatnot
|
||||||
// Chooser.djobj.addListener(this);
|
Chooser.djobj.addListener(this);
|
||||||
|
updateButtons();
|
||||||
|
|
||||||
// create our controller
|
// create our controller
|
||||||
_controller = new ChooserController();
|
_controller = new ChooserController();
|
||||||
@@ -158,10 +162,15 @@ public class ChooserFrame extends JFrame
|
|||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: convert to dobj biz
|
// documentation inherited from interface
|
||||||
public void playingUpdated (int songid, boolean paused)
|
public void attributeChanged (AttributeChangedEvent event)
|
||||||
{
|
{
|
||||||
if (songid == -1 || paused) {
|
updateButtons();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void updateButtons ()
|
||||||
|
{
|
||||||
|
if (Chooser.djobj.playing == -1 || Chooser.djobj.paused) {
|
||||||
_pause.setToolTipText(PLAY_TIP);
|
_pause.setToolTipText(PLAY_TIP);
|
||||||
_pause.setActionCommand("play");
|
_pause.setActionCommand("play");
|
||||||
_pause.setIcon(_playIcon);
|
_pause.setIcon(_playIcon);
|
||||||
@@ -172,7 +181,8 @@ public class ChooserFrame extends JFrame
|
|||||||
_pause.setIcon(_pauseIcon);
|
_pause.setIcon(_pauseIcon);
|
||||||
}
|
}
|
||||||
|
|
||||||
_stop.setEnabled(songid != -1);
|
_stop.setEnabled(Chooser.djobj.playing != -1);
|
||||||
|
_pause.setEnabled(Chooser.djobj.playlist != null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Our top-level controller. */
|
/** Our top-level controller. */
|
||||||
|
|||||||
@@ -22,6 +22,9 @@ public class DJObject extends DObject
|
|||||||
/** The index of the currently playing track. */
|
/** The index of the currently playing track. */
|
||||||
public int playing = -1;
|
public int playing = -1;
|
||||||
|
|
||||||
|
/** Whether or not we're paused. */
|
||||||
|
public boolean paused;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the currently playing track.
|
* Returns the currently playing track.
|
||||||
*/
|
*/
|
||||||
@@ -35,7 +38,7 @@ public class DJObject extends DObject
|
|||||||
*/
|
*/
|
||||||
public PlaylistEntry getNext ()
|
public PlaylistEntry getNext ()
|
||||||
{
|
{
|
||||||
return getEntry(playing + 1);
|
return (playing < 0) ? null : getEntry(playing + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -60,10 +63,12 @@ public class DJObject extends DObject
|
|||||||
public void play ()
|
public void play ()
|
||||||
{
|
{
|
||||||
if (havePlaylist()) {
|
if (havePlaylist()) {
|
||||||
// TODO: handle unpausing
|
|
||||||
if (playing == -1) {
|
if (playing == -1) {
|
||||||
setPlaying(0);
|
setPlaying(0);
|
||||||
}
|
}
|
||||||
|
if (paused) {
|
||||||
|
setPaused(false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -88,7 +93,7 @@ public class DJObject extends DObject
|
|||||||
|
|
||||||
public void pause ()
|
public void pause ()
|
||||||
{
|
{
|
||||||
// TODO
|
setPaused(!paused);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void remove (int start, int length)
|
public void remove (int start, int length)
|
||||||
|
|||||||
@@ -22,12 +22,18 @@ public class DJObject extends DObject
|
|||||||
/** The field name of the <code>playing</code> field. */
|
/** The field name of the <code>playing</code> field. */
|
||||||
public static final String PLAYING = "playing";
|
public static final String PLAYING = "playing";
|
||||||
|
|
||||||
|
/** The field name of the <code>paused</code> field. */
|
||||||
|
public static final String PAUSED = "paused";
|
||||||
|
|
||||||
/** The current playlist. */
|
/** The current playlist. */
|
||||||
public PlaylistEntry[] playlist;
|
public PlaylistEntry[] playlist;
|
||||||
|
|
||||||
/** The index of the currently playing track. */
|
/** The index of the currently playing track. */
|
||||||
public int playing = -1;
|
public int playing = -1;
|
||||||
|
|
||||||
|
/** Whether or not we're paused. */
|
||||||
|
public boolean paused;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the currently playing track.
|
* Returns the currently playing track.
|
||||||
*/
|
*/
|
||||||
@@ -41,7 +47,7 @@ public class DJObject extends DObject
|
|||||||
*/
|
*/
|
||||||
public PlaylistEntry getNext ()
|
public PlaylistEntry getNext ()
|
||||||
{
|
{
|
||||||
return getEntry(playing + 1);
|
return (playing < 0) ? null : getEntry(playing + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -66,10 +72,12 @@ public class DJObject extends DObject
|
|||||||
public void play ()
|
public void play ()
|
||||||
{
|
{
|
||||||
if (havePlaylist()) {
|
if (havePlaylist()) {
|
||||||
// TODO: handle unpausing
|
|
||||||
if (playing == -1) {
|
if (playing == -1) {
|
||||||
setPlaying(0);
|
setPlaying(0);
|
||||||
}
|
}
|
||||||
|
if (paused) {
|
||||||
|
setPaused(false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -94,7 +102,7 @@ public class DJObject extends DObject
|
|||||||
|
|
||||||
public void pause ()
|
public void pause ()
|
||||||
{
|
{
|
||||||
// TODO
|
setPaused(!paused);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void remove (int start, int length)
|
public void remove (int start, int length)
|
||||||
@@ -173,4 +181,18 @@ public class DJObject extends DObject
|
|||||||
requestAttributeChange(PLAYING, new Integer(playing));
|
requestAttributeChange(PLAYING, new Integer(playing));
|
||||||
this.playing = playing;
|
this.playing = playing;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Requests that the <code>paused</code> field be set to the specified
|
||||||
|
* value. The local value will be updated immediately and an event
|
||||||
|
* will be propagated through the system to notify all listeners that
|
||||||
|
* the attribute did change. Proxied copies of this object (on
|
||||||
|
* clients) will apply the value change when they received the
|
||||||
|
* attribute changed notification.
|
||||||
|
*/
|
||||||
|
public void setPaused (boolean paused)
|
||||||
|
{
|
||||||
|
requestAttributeChange(PAUSED, new Boolean(paused));
|
||||||
|
this.paused = paused;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,6 +44,12 @@ public class MusicPlayer extends Thread
|
|||||||
|
|
||||||
} else if (event.getName().equals(DJObject.PLAYLIST)) {
|
} else if (event.getName().equals(DJObject.PLAYLIST)) {
|
||||||
recomputeNext();
|
recomputeNext();
|
||||||
|
|
||||||
|
} else if (event.getName().equals(DJObject.PAUSED)) {
|
||||||
|
// wake the music thread if necessary
|
||||||
|
synchronized (this) {
|
||||||
|
notify();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,7 +61,7 @@ public class MusicPlayer extends Thread
|
|||||||
Player player = getPlaying();
|
Player player = getPlaying();
|
||||||
Log.info("Playing " + _pentry + "...");
|
Log.info("Playing " + _pentry + "...");
|
||||||
while (player.play(PLAY_FRAMES)) {
|
while (player.play(PLAY_FRAMES)) {
|
||||||
// loop!
|
checkPaused();
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
@@ -83,10 +89,18 @@ public class MusicPlayer extends Thread
|
|||||||
_fnentry = entry;
|
_fnentry = entry;
|
||||||
_fnext = getPlayer(_fnentry);
|
_fnext = getPlayer(_fnentry);
|
||||||
|
|
||||||
|
// if we're clearing the current entry, clear next as well
|
||||||
|
if (entry == null && _next != null) {
|
||||||
|
_next.close();
|
||||||
|
_next = null;
|
||||||
|
_nentry = null;
|
||||||
|
}
|
||||||
|
|
||||||
// clear out any current track
|
// clear out any current track
|
||||||
if (_playing != null) {
|
if (_playing != null) {
|
||||||
Log.info("Stopping current track.");
|
Log.info("Stopping current track.");
|
||||||
_playing.close();
|
_playing.close();
|
||||||
|
_pentry = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// finally wake up the player in case it is sleeping
|
// finally wake up the player in case it is sleeping
|
||||||
@@ -169,6 +183,17 @@ public class MusicPlayer extends Thread
|
|||||||
return _playing;
|
return _playing;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected synchronized void checkPaused ()
|
||||||
|
{
|
||||||
|
while (_djobj.paused) {
|
||||||
|
try {
|
||||||
|
wait();
|
||||||
|
} catch (InterruptedException ie) {
|
||||||
|
Log.warning("Interrupted waiting for unpause.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** Helper function. */
|
/** Helper function. */
|
||||||
protected boolean equals (PlaylistEntry e1, PlaylistEntry e2)
|
protected boolean equals (PlaylistEntry e1, PlaylistEntry e2)
|
||||||
{
|
{
|
||||||
@@ -185,5 +210,5 @@ public class MusicPlayer extends Thread
|
|||||||
protected PlaylistEntry _nentry, _fnentry;
|
protected PlaylistEntry _nentry, _fnentry;
|
||||||
|
|
||||||
/** The number of frames to play before checking up on things. */
|
/** The number of frames to play before checking up on things. */
|
||||||
protected static final int PLAY_FRAMES = 10;
|
protected static final int PLAY_FRAMES = 1;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user