Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pc.SoundInstance end event suspension #4234

Merged
merged 1 commit into from
May 11, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions src/sound/instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,14 @@ class SoundInstance extends EventHandler {
this._suspended = false;

/**
* True if we want to suspend the event handled to the 'onended' event.
* Greater than 0 if we want to suspend the event handled to the 'onended' event.
* When an 'onended' event is suspended, this counter is decremented by 1.
* When a future 'onended' event is to be suspended, this counter is incremented by 1.
*
* @type {boolean}
* @type {number}
* @private
*/
this._suspendEndEvent = false;
this._suspendEndEvent = 0;

/**
* True if we want to suspend firing instance events.
Expand Down Expand Up @@ -478,10 +480,10 @@ class SoundInstance extends EventHandler {
/** @private */
_onEnded() {
// the callback is not fired synchronously
// so only reset _suspendEndEvent to false when the
// so only decrement _suspendEndEvent when the
// callback is fired
if (this._suspendEndEvent) {
this._suspendEndEvent = false;
if (this._suspendEndEvent > 0) {
this._suspendEndEvent--;
return;
}

Expand Down Expand Up @@ -620,7 +622,7 @@ class SoundInstance extends EventHandler {

// Stop the source and re-create it because we cannot reuse the same source.
// Suspend the end event as we are manually stopping the source
this._suspendEndEvent = true;
this._suspendEndEvent++;
this.source.stop(0);
this.source = null;

Expand Down Expand Up @@ -710,7 +712,7 @@ class SoundInstance extends EventHandler {

this._startOffset = null;

this._suspendEndEvent = true;
this._suspendEndEvent++;
if (this._state === STATE_PLAYING) {
this.source.stop(0);
}
Expand Down Expand Up @@ -915,7 +917,7 @@ if (!hasAudioContext()) {
if (!this.source || this._state !== STATE_PLAYING)
return false;

this._suspendEndEvent = true;
this._suspendEndEvent++;
this.source.pause();
this._playWhenLoaded = false;
this._state = STATE_PAUSED;
Expand Down Expand Up @@ -952,7 +954,7 @@ if (!hasAudioContext()) {
this._manager.off('resume', this._onManagerResume, this);
this._manager.off('destroy', this._onManagerDestroy, this);

this._suspendEndEvent = true;
this._suspendEndEvent++;
this.source.pause();
this._playWhenLoaded = false;
this._state = STATE_STOPPED;
Expand Down