mirror of
https://github.com/ppy/osu.git
synced 2025-03-05 14:22:55 +08:00
Make PreviewTrackManager a Component in order to use DI
This commit is contained in:
parent
7cffabf7f9
commit
ad50f7faf1
@ -1,60 +1,66 @@
|
|||||||
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
|
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
|
||||||
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
||||||
|
|
||||||
|
using System;
|
||||||
using osu.Framework.Allocation;
|
using osu.Framework.Allocation;
|
||||||
using osu.Framework.Audio;
|
using osu.Framework.Audio;
|
||||||
using osu.Framework.Audio.Track;
|
using osu.Framework.Audio.Track;
|
||||||
using osu.Framework.Configuration;
|
using osu.Framework.Configuration;
|
||||||
|
using osu.Framework.Graphics;
|
||||||
using osu.Framework.IO.Stores;
|
using osu.Framework.IO.Stores;
|
||||||
|
using osu.Game.Beatmaps;
|
||||||
|
|
||||||
namespace osu.Game.Audio
|
namespace osu.Game.Audio
|
||||||
{
|
{
|
||||||
public class PreviewTrackManager : TrackManager
|
public class PreviewTrackManager : Component
|
||||||
{
|
{
|
||||||
private AudioManager audio;
|
public event Action PlaybackStarted;
|
||||||
private Track currentTrack;
|
public event Action PlaybackStopped;
|
||||||
private readonly BindableDouble muteBindable;
|
|
||||||
|
|
||||||
public PreviewTrackManager()
|
private TrackManager trackManager;
|
||||||
: base(new OnlineStore())
|
private BindableDouble muteBindable;
|
||||||
{
|
|
||||||
muteBindable = new BindableDouble();
|
public Track CurrentTrack { get; private set; }
|
||||||
}
|
|
||||||
|
|
||||||
[BackgroundDependencyLoader]
|
[BackgroundDependencyLoader]
|
||||||
private void load(AudioManager audio, FrameworkConfigManager config)
|
private void load(AudioManager audio, FrameworkConfigManager config)
|
||||||
{
|
{
|
||||||
this.audio = audio;
|
trackManager = new TrackManager(new OnlineStore());
|
||||||
|
|
||||||
audio.AddItem(this);
|
muteBindable = new BindableDouble();
|
||||||
|
|
||||||
config.BindWith(FrameworkSetting.VolumeMusic, Volume);
|
audio.AddItem(trackManager);
|
||||||
|
config.BindWith(FrameworkSetting.VolumeMusic, trackManager.Volume);
|
||||||
|
|
||||||
|
PlaybackStarted += () => audio.Track.AddAdjustment(AdjustableProperty.Volume, muteBindable);
|
||||||
|
PlaybackStopped += () => audio.Track.RemoveAdjustment(AdjustableProperty.Volume, muteBindable);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void UpdateState()
|
public Track Get(BeatmapSetInfo beatmapSetInfo) => trackManager.Get($"https://b.ppy.sh/preview/{beatmapSetInfo.OnlineBeatmapSetID}.mp3");
|
||||||
{
|
|
||||||
if (currentTrack?.HasCompleted ?? false)
|
|
||||||
onStop();
|
|
||||||
|
|
||||||
base.UpdateState();
|
protected override void Update()
|
||||||
|
{
|
||||||
|
if (CurrentTrack?.HasCompleted ?? false)
|
||||||
|
PlaybackStopped?.Invoke();
|
||||||
|
|
||||||
|
base.Update();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Play(Track track)
|
public void Play(Track track)
|
||||||
{
|
{
|
||||||
currentTrack?.Stop();
|
Stop();
|
||||||
currentTrack = track;
|
CurrentTrack = track;
|
||||||
currentTrack.Restart();
|
track.Restart();
|
||||||
onPlay();
|
PlaybackStarted?.Invoke();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onPlay() => audio.Track.AddAdjustment(AdjustableProperty.Volume, muteBindable);
|
|
||||||
|
|
||||||
public void Stop()
|
public void Stop()
|
||||||
{
|
{
|
||||||
currentTrack?.Stop();
|
if (CurrentTrack?.IsRunning ?? false)
|
||||||
onStop();
|
{
|
||||||
|
CurrentTrack?.Stop();
|
||||||
|
PlaybackStopped?.Invoke();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onStop() => audio.Track.RemoveAdjustment(AdjustableProperty.Volume, muteBindable);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user