Added a method for playing sounds after a specified delay.

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@2826 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Ray Greenwell
2003-10-16 01:01:51 +00:00
parent a02d076d8e
commit 969008a6b8
@@ -1,5 +1,5 @@
// //
// $Id: SoundManager.java,v 1.65 2003/07/28 05:32:04 ray Exp $ // $Id: SoundManager.java,v 1.66 2003/10/16 01:01:51 ray Exp $
package com.threerings.media.sound; package com.threerings.media.sound;
@@ -37,6 +37,8 @@ import org.apache.commons.io.StreamUtils;
import org.apache.commons.lang.Constant; import org.apache.commons.lang.Constant;
import com.samskivert.util.Config; import com.samskivert.util.Config;
import com.samskivert.util.Interval;
import com.samskivert.util.IntervalManager;
import com.samskivert.util.LRUHashMap; import com.samskivert.util.LRUHashMap;
import com.samskivert.util.Queue; import com.samskivert.util.Queue;
import com.samskivert.util.RuntimeAdjust; import com.samskivert.util.RuntimeAdjust;
@@ -341,10 +343,19 @@ public class SoundManager
} }
/** /**
* Play the specified sound of as the specified type of sound. * Play the specified sound of as the specified type of sound, immediately.
* Note that a sound need not be locked prior to playing. * Note that a sound need not be locked prior to playing.
*/ */
public void play (SoundType type, String pkgPath, String key) public void play (SoundType type, String pkgPath, String key)
{
play(type, pkgPath, key, 0);
}
/**
* Play the specified sound after the specified delay.
* @param delay the delay in milliseconds.
*/
public void play (SoundType type, String pkgPath, String key, int delay)
{ {
if (!SOUND_ENABLED) return; if (!SOUND_ENABLED) return;
@@ -354,22 +365,34 @@ public class SoundManager
} }
if (_player != null && (_clipVol != 0f) && isEnabled(type)) { if (_player != null && (_clipVol != 0f) && isEnabled(type)) {
SoundKey skey = new SoundKey(pkgPath, key); SoundKey skey = new SoundKey(pkgPath, key, delay);
synchronized (_queue) { if (delay > 0) {
if (_queue.size() < MAX_QUEUE_SIZE) { IntervalManager.register(_playLater, delay, skey, false);
/* } else {
if (_verbose.getValue()) { addToPlayQueue(skey);
Log.info("Sound request [key=" + skey + "]."); }
} }
*/ }
_queue.append(PLAY);
_queue.append(skey);
} else if (_verbose.getValue()) { /**
Log.warning("SoundManager not playing sound because " + * Add the sound clip key to the queue to be played.
"too many sounds in queue [key=" + skey + */
", type=" + type + ", queue=" + _queue + "]."); protected void addToPlayQueue (SoundKey skey)
{
synchronized (_queue) {
if (_queue.size() < MAX_QUEUE_SIZE) {
/*
if (_verbose.getValue()) {
Log.info("Sound request [key=" + skey + "].");
} }
*/
_queue.append(PLAY);
_queue.append(skey);
} else if (_verbose.getValue()) {
Log.warning("SoundManager not playing sound because " +
"too many sounds in queue [key=" + skey +
", queue=" + _queue + "].");
} }
} }
} }
@@ -1118,12 +1141,23 @@ public class SoundManager
public long stamp; public long stamp;
/**
* Quicky constructor for music keys and lock operations.
*/
public SoundKey (String pkgPath, String key) public SoundKey (String pkgPath, String key)
{ {
this.pkgPath = pkgPath; this.pkgPath = pkgPath;
this.key = key; this.key = key;
}
stamp = System.currentTimeMillis(); /**
* Constructor for a sound effect soundkey.
*/
public SoundKey (String pkgPath, String key, int delay)
{
this(pkgPath, key);
stamp = System.currentTimeMillis() + delay;
} }
/** /**
@@ -1186,6 +1220,13 @@ public class SoundManager
/** The queue of sound clips to be played. */ /** The queue of sound clips to be played. */
protected Queue _queue = new Queue(); protected Queue _queue = new Queue();
/** An interval that plays sounds later. */
protected Interval _playLater = new Interval() {
public void intervalExpired (int id, Object rock) {
addToPlayQueue((SoundKey) rock);
}
};
/** Volume levels for both sound clips and music. */ /** Volume levels for both sound clips and music. */
protected float _clipVol = 1f, _musicVol = 1f; protected float _clipVol = 1f, _musicVol = 1f;