1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-27 11:12:59 +08:00

Stop playing samples on pause, resume looping on unpause

This commit is contained in:
Dean Herbert 2020-07-27 16:15:49 +09:00
parent 3257c1e9f2
commit 9889bfa0f3

View File

@ -12,6 +12,7 @@ using osu.Framework.Graphics.Audio;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Transforms; using osu.Framework.Graphics.Transforms;
using osu.Game.Audio; using osu.Game.Audio;
using osu.Game.Screens.Play;
namespace osu.Game.Skinning namespace osu.Game.Skinning
{ {
@ -22,9 +23,13 @@ namespace osu.Game.Skinning
[Resolved] [Resolved]
private ISampleStore samples { get; set; } private ISampleStore samples { get; set; }
private bool requestedPlaying;
public override bool RemoveWhenNotAlive => false; public override bool RemoveWhenNotAlive => false;
public override bool RemoveCompletedTransforms => false; public override bool RemoveCompletedTransforms => false;
private readonly AudioContainer<DrawableSample> samplesContainer;
public SkinnableSound(ISampleInfo hitSamples) public SkinnableSound(ISampleInfo hitSamples)
: this(new[] { hitSamples }) : this(new[] { hitSamples })
{ {
@ -36,9 +41,28 @@ namespace osu.Game.Skinning
InternalChild = samplesContainer = new AudioContainer<DrawableSample>(); InternalChild = samplesContainer = new AudioContainer<DrawableSample>();
} }
private bool looping; private Bindable<bool> gameplayClockPaused;
private readonly AudioContainer<DrawableSample> samplesContainer; [BackgroundDependencyLoader(true)]
private void load(GameplayClock gameplayClock)
{
// if in a gameplay context, pause sample playback when gameplay is paused.
gameplayClockPaused = gameplayClock?.IsPaused.GetBoundCopy();
gameplayClockPaused?.BindValueChanged(paused =>
{
if (requestedPlaying)
{
if (paused.NewValue)
stop();
// it's not easy to know if a sample has finished playing (to end).
// to keep things simple only resume playing looping samples.
else if (Looping)
play();
}
});
}
private bool looping;
public bool Looping public bool Looping
{ {
@ -53,20 +77,36 @@ namespace osu.Game.Skinning
} }
} }
public void Play() => samplesContainer.ForEach(c => public void Play()
{ {
if (c.AggregateVolume.Value > 0) requestedPlaying = true;
c.Play(); play();
}); }
public void Stop() => samplesContainer.ForEach(c => c.Stop()); private void play()
{
samplesContainer.ForEach(c =>
{
if (c.AggregateVolume.Value > 0)
c.Play();
});
}
public void Stop()
{
requestedPlaying = false;
stop();
}
private void stop()
{
samplesContainer.ForEach(c => c.Stop());
}
public override bool IsPresent => Scheduler.HasPendingTasks; public override bool IsPresent => Scheduler.HasPendingTasks;
protected override void SkinChanged(ISkinSource skin, bool allowFallback) protected override void SkinChanged(ISkinSource skin, bool allowFallback)
{ {
bool wasPlaying = samplesContainer.Any(s => s.Playing);
var channels = hitSamples.Select(s => var channels = hitSamples.Select(s =>
{ {
var ch = skin.GetSample(s); var ch = skin.GetSample(s);
@ -91,7 +131,7 @@ namespace osu.Game.Skinning
samplesContainer.ChildrenEnumerable = channels.Select(c => new DrawableSample(c)); samplesContainer.ChildrenEnumerable = channels.Select(c => new DrawableSample(c));
if (wasPlaying) if (requestedPlaying)
Play(); Play();
} }