1
0
Fork 0
mirror of https://github.com/ninjamuffin99/Funkin.git synced 2024-12-13 08:55:29 +00:00

Merge pull request #330 from FunkinCrew/bugfix/audio-focus

Fix `FunkinSound` not resuming after focus
This commit is contained in:
Eric 2024-03-04 22:57:39 -05:00 committed by GitHub
commit c6ad7f365f

View file

@ -90,6 +90,11 @@ class FunkinSound extends FlxSound implements ICloneable<FunkinSound>
*/ */
var _label:String = "unknown"; var _label:String = "unknown";
/**
* Whether we received a focus lost event.
*/
var _lostFocus:Bool = false;
public function new() public function new()
{ {
super(); super();
@ -167,8 +172,18 @@ class FunkinSound extends FlxSound implements ICloneable<FunkinSound>
public override function pause():FunkinSound public override function pause():FunkinSound
{ {
super.pause(); if (_shouldPlay)
this._shouldPlay = false; {
// This sound will eventually play, but is still at a negative timestamp.
// Manually set the paused flag to ensure proper focus/unfocus behavior.
_shouldPlay = false;
_paused = true;
active = false;
}
else
{
super.pause();
}
return this; return this;
} }
@ -177,7 +192,10 @@ class FunkinSound extends FlxSound implements ICloneable<FunkinSound>
*/ */
override function onFocus():Void override function onFocus():Void
{ {
if (!_alreadyPaused) // Flixel can sometimes toss spurious `onFocus` events, e.g. if the Flixel debugger is toggled
// on and off. We only want to resume the sound if we actually lost focus, and if we weren't
// already paused before we lost focus.
if (_lostFocus && !_alreadyPaused)
{ {
resume(); resume();
} }
@ -185,6 +203,7 @@ class FunkinSound extends FlxSound implements ICloneable<FunkinSound>
{ {
trace('Not resuming audio on focus!'); trace('Not resuming audio on focus!');
} }
_lostFocus = false;
} }
/** /**
@ -193,6 +212,7 @@ class FunkinSound extends FlxSound implements ICloneable<FunkinSound>
override function onFocusLost():Void override function onFocusLost():Void
{ {
trace('Focus lost, pausing audio!'); trace('Focus lost, pausing audio!');
_lostFocus = true;
_alreadyPaused = _paused; _alreadyPaused = _paused;
pause(); pause();
} }
@ -201,7 +221,10 @@ class FunkinSound extends FlxSound implements ICloneable<FunkinSound>
{ {
if (this._time < 0) if (this._time < 0)
{ {
this._shouldPlay = true; // Sound with negative timestamp, restart the timer.
_shouldPlay = true;
_paused = false;
active = true;
} }
else else
{ {