mirror of
https://github.com/ppy/osu.git
synced 2025-01-12 12:22:56 +08:00
Stop playing samples on pause, resume looping on unpause
This commit is contained in:
parent
3257c1e9f2
commit
9889bfa0f3
@ -12,6 +12,7 @@ using osu.Framework.Graphics.Audio;
|
||||
using osu.Framework.Graphics.Containers;
|
||||
using osu.Framework.Graphics.Transforms;
|
||||
using osu.Game.Audio;
|
||||
using osu.Game.Screens.Play;
|
||||
|
||||
namespace osu.Game.Skinning
|
||||
{
|
||||
@ -22,9 +23,13 @@ namespace osu.Game.Skinning
|
||||
[Resolved]
|
||||
private ISampleStore samples { get; set; }
|
||||
|
||||
private bool requestedPlaying;
|
||||
|
||||
public override bool RemoveWhenNotAlive => false;
|
||||
public override bool RemoveCompletedTransforms => false;
|
||||
|
||||
private readonly AudioContainer<DrawableSample> samplesContainer;
|
||||
|
||||
public SkinnableSound(ISampleInfo hitSamples)
|
||||
: this(new[] { hitSamples })
|
||||
{
|
||||
@ -36,9 +41,28 @@ namespace osu.Game.Skinning
|
||||
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
|
||||
{
|
||||
@ -53,20 +77,36 @@ namespace osu.Game.Skinning
|
||||
}
|
||||
}
|
||||
|
||||
public void Play() => samplesContainer.ForEach(c =>
|
||||
public void Play()
|
||||
{
|
||||
if (c.AggregateVolume.Value > 0)
|
||||
c.Play();
|
||||
});
|
||||
requestedPlaying = true;
|
||||
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;
|
||||
|
||||
protected override void SkinChanged(ISkinSource skin, bool allowFallback)
|
||||
{
|
||||
bool wasPlaying = samplesContainer.Any(s => s.Playing);
|
||||
|
||||
var channels = hitSamples.Select(s =>
|
||||
{
|
||||
var ch = skin.GetSample(s);
|
||||
@ -91,7 +131,7 @@ namespace osu.Game.Skinning
|
||||
|
||||
samplesContainer.ChildrenEnumerable = channels.Select(c => new DrawableSample(c));
|
||||
|
||||
if (wasPlaying)
|
||||
if (requestedPlaying)
|
||||
Play();
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user