Skip to content

Commit

Permalink
Merge pull request #15821 from peppy/reapply-preview-track-workaround
Browse files Browse the repository at this point in the history
Fix potential crashes when playing preview tracks in single thread mode
  • Loading branch information
smoogipoo authored Nov 26, 2021
2 parents 35a347c + f16ef1e commit ed5f703
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions osu.Game/Beatmaps/Drawables/Cards/Buttons/PlayButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ private void updateState(ValueChangedEvent<bool> playing)
if (previewTrack == null)
{
toggleLoading(true);

LoadComponentAsync(previewTrack = previewTrackManager.Get(beatmapSetInfo), onPreviewLoaded);
}
else
Expand All @@ -112,18 +113,23 @@ private void stopPreview()

private void onPreviewLoaded(PreviewTrack loadedPreview)
{
// another async load might have completed before this one.
// if so, do not make any changes.
if (loadedPreview != previewTrack)
return;
// Make sure that we schedule to after the next audio frame to fix crashes in single-threaded execution.
// See: https://github.com/ppy/osu-framework/issues/4692
Schedule(() =>
{
// another async load might have completed before this one.
// if so, do not make any changes.
if (loadedPreview != previewTrack)
return;

AddInternal(loadedPreview);
toggleLoading(false);
AddInternal(loadedPreview);
toggleLoading(false);

loadedPreview.Stopped += () => Schedule(() => Playing.Value = false);
loadedPreview.Stopped += () => Schedule(() => Playing.Value = false);

if (Playing.Value)
tryStartPreview();
if (Playing.Value)
tryStartPreview();
});
}

private void tryStartPreview()
Expand Down

0 comments on commit ed5f703

Please sign in to comment.