Fix pc.SoundInstance end event suspension #4234
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When the currentTime value of a SoundInstance is quickly changed, the sound can stop unintentionally. Setting currentTime first stops the playback, sets the playback offset and starts the playback again. Stopping is asynchronous but currentTime doesn't wait for the playback to be stopped, so multiple end events may be fired at once. To suspend end events, a flag is set to true so that the next end event is ignored, however, because multiple end events may happen at once, only one of the events is suspended, leading to the playback unintentionally being stopped. To fix this issue, a counter is used instead of a flag: increment when the next end event needs to be ignored, decrement when an event was ignored, don't do anything if the counter is 0
Tested on Firefox 100.0 Linux x64
Example project demonstrating bug: https://playcanvas.com/project/924909/overview/currenttime-bug
I confirm I have read the contributing guidelines and signed the Contributor License Agreement.