From f49b351b27e496a8aa6be4a9cded67f69592ace7 Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Fri, 12 Feb 2010 01:09:54 +0000 Subject: [PATCH] We had a problem where we would start the sound (allowing for deferred play) and then quickly stop it, so that the sound would resolve (and thus start playing) after the call to stop. To avoid that, store the desired state if the sound is not yet resolved and check it when the resolution finishes. git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@885 ed5b42cb-e716-0410-a449-f6a68f950b19 --- src/java/com/threerings/openal/Sound.java | 26 +++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/java/com/threerings/openal/Sound.java b/src/java/com/threerings/openal/Sound.java index ce954144..c29ed4cd 100644 --- a/src/java/com/threerings/openal/Sound.java +++ b/src/java/com/threerings/openal/Sound.java @@ -21,6 +21,8 @@ package com.threerings.openal; +import org.lwjgl.openal.AL10; + /** * Represents an instance of a sound clip which can be positioned in 3D space, gain and pitch * adjusted and played or looped. @@ -267,6 +269,8 @@ public class Sound { if (_source != null) { _source.pause(); + } else { + _stateDesired = AL10.AL_PAUSED; } } @@ -278,6 +282,8 @@ public class Sound { if (_source != null) { _source.stop(); + } else { + _stateDesired = AL10.AL_STOPPED; } } @@ -308,15 +314,25 @@ public class Sound // if we're not ready to go... if (!_buffer.isPlayable()) { if (allowDefer) { + // save the desired state, which may be overridden by calls to play/pause/stop + _stateDesired = AL10.AL_PLAYING; + _loopDesired = loop; + // resolve the buffer and instruct it to play once it is resolved _buffer.resolve(new ClipBuffer.Observer() { public void clipLoaded (ClipBuffer buffer) { - play(false, loop, obs); + if (_stateDesired == AL10.AL_STOPPED) { + return; + } + play(false, _loopDesired, obs); + if (_stateDesired == AL10.AL_PAUSED) { + pause(); + } } public void clipFailed (ClipBuffer buffer) { // well, let's pretend like the sound started so that the observer isn't // left hanging - if (obs != null) { + if (obs != null && _stateDesired != AL10.AL_STOPPED) { obs.soundStarted(Sound.this); } } @@ -399,6 +415,12 @@ public class Sound /** The source via which we are playing our sound currently. */ protected Source _source; + /** The desired state of the sound (stopped, playing, paused) after resolution. */ + protected int _stateDesired; + + /** Whether or not looping is desired after resolution. */ + protected boolean _loopDesired; + /** The position of the sound. */ protected float _px, _py, _pz;