From f086be9c189cba2e562ee66c3f451a20777eb65d Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Wed, 4 Jun 2025 13:55:25 +0900 Subject: [PATCH] Fix audio not correctly continuing on resuming from gameplay --- .../Navigation/TestSceneScreenNavigation.cs | 8 +++ .../TestSceneSongSelectNavigation.cs | 61 +++++++++++++++++++ osu.Game/Screens/SelectV2/SongSelect.cs | 25 +++++--- 3 files changed, 84 insertions(+), 10 deletions(-) create mode 100644 osu.Game.Tests/Visual/Navigation/TestSceneSongSelectNavigation.cs diff --git a/osu.Game.Tests/Visual/Navigation/TestSceneScreenNavigation.cs b/osu.Game.Tests/Visual/Navigation/TestSceneScreenNavigation.cs index 7aa2ecb06c..e2b2e41456 100644 --- a/osu.Game.Tests/Visual/Navigation/TestSceneScreenNavigation.cs +++ b/osu.Game.Tests/Visual/Navigation/TestSceneScreenNavigation.cs @@ -796,6 +796,14 @@ namespace osu.Game.Tests.Visual.Navigation AddWaitStep("wait two frames", 2); } + [Test] + public void TestPushSongSelectAndPressBackButtonImmediatelyV2() + { + AddStep("push song select", () => Game.ScreenStack.Push(new TestPlaySongSelect())); + AddStep("press back button", () => Game.ChildrenOfType().First().Action!.Invoke()); + AddWaitStep("wait two frames", 2); + } + [Test] public void TestExitSongSelectWithClick() { diff --git a/osu.Game.Tests/Visual/Navigation/TestSceneSongSelectNavigation.cs b/osu.Game.Tests/Visual/Navigation/TestSceneSongSelectNavigation.cs new file mode 100644 index 0000000000..f369a52ae7 --- /dev/null +++ b/osu.Game.Tests/Visual/Navigation/TestSceneSongSelectNavigation.cs @@ -0,0 +1,61 @@ +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +using NUnit.Framework; +using osu.Framework.Allocation; +using osu.Framework.Extensions; +using osu.Game.Beatmaps; +using osu.Game.Overlays; +using osu.Game.Screens.Play; +using osu.Game.Screens.SelectV2; +using osu.Game.Tests.Beatmaps.IO; +using osuTK.Input; + +namespace osu.Game.Tests.Visual.Navigation +{ + /// + /// Tests copied out of `TestSceneScreenNavigation` which are specific to song select. + /// These are for SongSelectV2. Eventually, the tests in the above class should be deleted along with old song select. + /// + public class TestSceneSongSelectNavigation : OsuGameTestScene + { + [TestCase(true)] + [TestCase(false)] + public void TestSongContinuesAfterExitPlayer(bool withUserPause) + { + Player? player = null; + + IWorkingBeatmap beatmap() => Game.Beatmap.Value; + + PushAndConfirm(() => new SoloSongSelect()); + + AddStep("import beatmap", () => BeatmapImportHelper.LoadOszIntoOsu(Game, virtualTrack: true).WaitSafely()); + + AddUntilStep("wait for selected", () => !Game.Beatmap.IsDefault); + + if (withUserPause) + AddStep("pause", () => Game.Dependencies.Get().Stop(true)); + + AddStep("press enter", () => InputManager.Key(Key.Enter)); + + AddUntilStep("wait for player", () => + { + DismissAnyNotifications(); + return (player = Game.ScreenStack.CurrentScreen as Player) != null; + }); + + AddUntilStep("wait for fail", () => player?.GameplayState.HasFailed, () => Is.True); + + AddUntilStep("wait for track stop", () => !Game.MusicController.IsPlaying); + AddAssert("Ensure time before preview point", () => Game.MusicController.CurrentTrack.CurrentTime < beatmap().BeatmapInfo.Metadata.PreviewTime); + + pushEscape(); + + AddUntilStep("wait for track playing", () => Game.MusicController.IsPlaying); + AddAssert("Ensure time wasn't reset to preview point", () => Game.MusicController.CurrentTrack.CurrentTime < beatmap().BeatmapInfo.Metadata.PreviewTime); + } + + private void pushEscape() => + AddStep("Press escape", () => InputManager.Key(Key.Escape)); + } +} diff --git a/osu.Game/Screens/SelectV2/SongSelect.cs b/osu.Game/Screens/SelectV2/SongSelect.cs index 28e11930da..2c382ecd7a 100644 --- a/osu.Game/Screens/SelectV2/SongSelect.cs +++ b/osu.Game/Screens/SelectV2/SongSelect.cs @@ -306,14 +306,10 @@ namespace osu.Game.Screens.SelectV2 Beatmap.BindValueChanged(_ => { ensureGlobalBeatmapValid(); - updateStateFromCurrentBeatmap(); - }); - } - private void updateStateFromCurrentBeatmap() - { - ensurePlayingSelected(); - updateBackgroundDim(); + ensurePlayingSelected(true); + updateBackgroundDim(); + }); } protected override void Update() @@ -334,7 +330,7 @@ namespace osu.Game.Screens.SelectV2 /// Ensures some music is playing for the current track. /// Will resume playback from a manual user pause if the track has changed. /// - private void ensurePlayingSelected() + private void ensurePlayingSelected(bool restart) { if (!ControlGlobalMusic) return; @@ -346,7 +342,7 @@ namespace osu.Game.Screens.SelectV2 if (!track.IsRunning && (music.UserPauseRequested != true || isNewTrack)) { Logger.Log($"Song select decided to {nameof(ensurePlayingSelected)}"); - music.Play(true); + music.Play(restart); } lastTrack.SetTarget(track); @@ -518,6 +514,14 @@ namespace osu.Game.Screens.SelectV2 this.FadeIn(fade_duration, Easing.OutQuint); onArrivingAtScreen(); + + if (ControlGlobalMusic) + { + // restart playback on returning to song select, regardless. + // not sure this should be a permanent thing (we may want to leave a user pause paused even on returning) + music.ResetTrackAdjustments(); + music.Play(requestedByUser: true); + } } public override void OnSuspending(ScreenTransitionEvent e) @@ -556,7 +560,8 @@ namespace osu.Game.Screens.SelectV2 ensureGlobalBeatmapValid(); - updateStateFromCurrentBeatmap(); + ensurePlayingSelected(false); + updateBackgroundDim(); } private void onLeavingScreen()