Skip to content

Commit

Permalink
Merge pull request #1736 from winterismute/issue-1662-loop-point
Browse files Browse the repository at this point in the history
FlxSound: add loopTime, closes #1662
  • Loading branch information
Gama11 committed Mar 15, 2016
2 parents 6136013 + c660ded commit 53c3981
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 30 deletions.
53 changes: 24 additions & 29 deletions flixel/system/FlxSound.hx
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ class FlxSound extends FlxBasic
*/
@:isVar
public var looped(get, set):Bool;
/**
* In case of looping, the point (in milliseconds) from where to restart the sound when it loops back
*/
public var loopTime(default, set):Float;
/**
* Internal tracker for a Flash sound object.
*/
Expand Down Expand Up @@ -169,6 +173,7 @@ class FlxSound extends FlxBasic
_volume = 1.0;
_volumeAdjust = 1.0;
looped = false;
loopTime = 0.0;
_target = null;
_radius = 0;
_proximityPan = false;
Expand Down Expand Up @@ -382,16 +387,17 @@ class FlxSound extends FlxBasic
* Call this function to play the sound - also works on paused sounds.
*
* @param ForceRestart Whether to start the sound over or not. Default value is false, meaning if the sound is already playing or was paused when you call play(), it will continue playing from its current position, NOT start again from the beginning.
* @param Time At which point to start plaing the sound, in milliseconds
*/
public function play(ForceRestart:Bool = false):FlxSound
public function play(ForceRestart:Bool = false, StartTime:Float = 0.0):FlxSound
{
if (!exists)
{
return this;
}
if (ForceRestart)
{
cleanup(false, true, true);
cleanup(false, true);
}
else if (playing)
{
Expand All @@ -405,7 +411,7 @@ class FlxSound extends FlxBasic
}
else
{
startSound(0);
startSound(StartTime);
}
return this;
}
Expand Down Expand Up @@ -433,7 +439,7 @@ class FlxSound extends FlxBasic
}
time = _channel.position;
_paused = true;
cleanup(false, false, false);
cleanup(false, false);
return this;
}

Expand All @@ -442,10 +448,16 @@ class FlxSound extends FlxBasic
*/
public inline function stop():FlxSound
{
cleanup(autoDestroy, true, true);
cleanup(autoDestroy, true);
return this;
}

private function set_loopTime(newloopTime:Float):Float
{
loopTime = newloopTime;
return loopTime;
}

/**
* Helper function that tweens this sound's volume.
*
Expand Down Expand Up @@ -523,18 +535,16 @@ class FlxSound extends FlxBasic
/**
* An internal helper function used to attempt to start playing the sound and populate the _channel variable.
*/
private function startSound(Position:Float):Void
private function startSound(StartTime:Float):Void
{
if (_sound == null)
{
return;
}

var numLoops:Int = looped && Position == 0 ? FlxMath.MAX_VALUE_INT : 0;

time = Position;
time = StartTime;
_paused = false;
_channel = _sound.play(time, numLoops, _transform);
_channel = _sound.play(time, 0, _transform);
if (_channel != null)
{
#if (sys && openfl_legacy)
Expand Down Expand Up @@ -563,7 +573,7 @@ class FlxSound extends FlxBasic
if (looped)
{
cleanup(false);
play();
play(false, loopTime);
}
else
{
Expand All @@ -576,9 +586,8 @@ class FlxSound extends FlxBasic
*
* @param destroySound Whether or not to destroy the sound. If this is true, the position and fading will be reset as well.
* @param resetPosition Whether or not to reset the position of the sound.
* @param resetFading Whether or not to reset the current fading variables of the sound.
*/
private function cleanup(destroySound:Bool, resetPosition:Bool = true, resetFading:Bool = true):Void
private function cleanup(destroySound:Bool, resetPosition:Bool = true):Void
{
if (destroySound)
{
Expand Down Expand Up @@ -703,22 +712,8 @@ class FlxSound extends FlxBasic

private inline function set_looped(loop:Bool):Bool
{
// If we're going from looping to not looping while playing,
// the channel needs to be updated so it won't loop next time.

if (!loop && looped && playing)
{
looped = loop;
var pos:Float = _channel.position;
cleanup(false, false, false);
startSound(pos);
}
else
{
looped = loop;
}

return loop;
looped = loop;
return looped;
}

override public function toString():String
Expand Down
4 changes: 3 additions & 1 deletion flixel/system/frontEnds/SoundFrontEnd.hx
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,9 @@ class SoundFrontEnd
* @param Volume How loud the sound should be, from 0 to 1.
* @param Looped Whether to loop this music.
* @param Group The group to add this sound to.
* @param LoopTime Point at which the sound will restart when looping back (in case of looping)
*/
public function playMusic(Music:FlxSoundAsset, Volume:Float = 1, Looped:Bool = true, ?Group:FlxSoundGroup):Void
public function playMusic(Music:FlxSoundAsset, Volume:Float = 1, Looped:Bool = true, ?Group:FlxSoundGroup, LoopTime:Float = 0.0):Void
{
if (music == null)
{
Expand All @@ -94,6 +95,7 @@ class SoundFrontEnd
music.volume = Volume;
music.persist = true;
music.group = (Group == null) ? defaultMusicGroup : Group;
music.loopTime = LoopTime;
music.play();
}

Expand Down

0 comments on commit 53c3981

Please sign in to comment.