Wow, when you tell a sound to loop, the position just continues

to increase, up past the duration. Seeking in those mode also
causes it to get wiggy and freak out. So don't have flash loop it,
but just loop it ourselves by autorewinding and playing again.
Also, just autorewind and pause if not looping, which I had
wanted to do anyway.


git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@709 ed5b42cb-e716-0410-a449-f6a68f950b19
This commit is contained in:
Ray Greenwell
2008-11-17 05:51:20 +00:00
parent 8cd31c9564
commit 5161e49bd4
@@ -42,9 +42,9 @@ public class Mp3AudioPlayer extends EventDispatcher
_isComplete = false; _isComplete = false;
_lastPosition = 0; _lastPosition = 0;
_sound = new Sound(); _sound = new Sound();
_sound.addEventListener(Event.ID3, handleId3); // TODO _sound.addEventListener(Event.ID3, handleId3);
_sound.addEventListener(IOErrorEvent.IO_ERROR, handleError); _sound.addEventListener(IOErrorEvent.IO_ERROR, handleError);
_sound.addEventListener(Event.COMPLETE, handleSoundComplete); _sound.addEventListener(Event.COMPLETE, handleLoadingComplete);
_sound.load(new URLRequest(url), new SoundLoaderContext(1000, true)); _sound.load(new URLRequest(url), new SoundLoaderContext(1000, true));
play(); play();
} }
@@ -66,7 +66,7 @@ public class Mp3AudioPlayer extends EventDispatcher
// from AudioPlayer // from AudioPlayer
public function pause () :void public function pause () :void
{ {
handlePositionCheck(null); // update _lastPosition and dispatch an event handlePositionCheck(); // update _lastPosition and dispatch an event
pause0(); pause0();
updateState(MediaPlayerCodes.STATE_PAUSED); updateState(MediaPlayerCodes.STATE_PAUSED);
} }
@@ -86,7 +86,8 @@ public class Mp3AudioPlayer extends EventDispatcher
if (_chan == null) { if (_chan == null) {
return _lastPosition; return _lastPosition;
} }
return _chan.position / 1000; // we mod by the duration because when we loop the position is ever-growing. hahah!
return (_chan.position / 1000);
} }
// from AudioPlayer // from AudioPlayer
@@ -98,7 +99,7 @@ public class Mp3AudioPlayer extends EventDispatcher
play0(); play0();
} else { } else {
handlePositionCheck(null); handlePositionCheck();
} }
} }
@@ -126,6 +127,7 @@ public class Mp3AudioPlayer extends EventDispatcher
// from AudioPlayer // from AudioPlayer
public function unload () :void public function unload () :void
{ {
pause0();
if (_sound != null) { if (_sound != null) {
try { try {
_sound.close(); _sound.close();
@@ -133,7 +135,6 @@ public class Mp3AudioPlayer extends EventDispatcher
// ignore // ignore
} }
} }
pause0();
_positionChecker.reset(); _positionChecker.reset();
_lastPosition = NaN; _lastPosition = NaN;
} }
@@ -144,9 +145,11 @@ public class Mp3AudioPlayer extends EventDispatcher
protected function play0 () :void protected function play0 () :void
{ {
pause0(); pause0();
_chan = _sound.play(_lastPosition * 1000, _loop ? int.MAX_VALUE : 0, _chan = _sound.play(_lastPosition * 1000, 0, new SoundTransform(_volume));
new SoundTransform(_volume)); if (_chan != null) {
if (_chan == null) { _chan.addEventListener(Event.SOUND_COMPLETE, handlePlaybackComplete);
} else {
Log.getLog(this).warning("All sound channels are in use; " + Log.getLog(this).warning("All sound channels are in use; " +
"unable to play sound."); "unable to play sound.");
} }
@@ -163,7 +166,7 @@ public class Mp3AudioPlayer extends EventDispatcher
} }
} }
protected function handlePositionCheck (event :TimerEvent) :void protected function handlePositionCheck (event :TimerEvent = null) :void
{ {
if (!_isComplete) { if (!_isComplete) {
dispatchEvent(new ValueEvent(MediaPlayerCodes.DURATION, getDuration())); dispatchEvent(new ValueEvent(MediaPlayerCodes.DURATION, getDuration()));
@@ -194,12 +197,24 @@ public class Mp3AudioPlayer extends EventDispatcher
/** /**
*/ */
protected function handleSoundComplete (event :Event) :void protected function handleLoadingComplete (event :Event) :void
{ {
handlePositionCheck(null); // also updates duration handlePositionCheck(); // also updates duration
_isComplete = true; _isComplete = true;
} }
protected function handlePlaybackComplete (event :Event) :void
{
_lastPosition = 0;
pause0();
handlePositionCheck();
if (_loop) {
play0();
} else {
updateState(MediaPlayerCodes.STATE_PAUSED);
}
}
protected function handleError (event :IOErrorEvent) :void protected function handleError (event :IOErrorEvent) :void
{ {
dispatchEvent(new ValueEvent(MediaPlayerCodes.ERROR, event.text)); dispatchEvent(new ValueEvent(MediaPlayerCodes.ERROR, event.text));