osu/osu.Game/Audio/PreviewTrackManager.cs

60 lines
1.9 KiB
C#
Raw Normal View History

2018-05-08 19:55:48 +00:00
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
2018-05-08 19:55:48 +00:00
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Audio.Track;
using osu.Framework.Configuration;
using osu.Framework.Graphics;
2018-05-08 19:55:48 +00:00
using osu.Framework.IO.Stores;
using osu.Game.Beatmaps;
2018-05-08 19:55:48 +00:00
namespace osu.Game.Audio
{
public class PreviewTrackManager : Component
2018-05-08 19:55:48 +00:00
{
2018-05-24 21:37:53 +00:00
private Action<PreviewTrack> onTrackStart;
private Action onTrackStop;
2018-05-08 19:55:48 +00:00
private TrackManager trackManager;
private BindableDouble muteBindable;
2018-05-24 21:37:53 +00:00
public PreviewTrack CurrentTrack { get; private set; }
2018-05-08 19:55:48 +00:00
[BackgroundDependencyLoader]
private void load(AudioManager audio, FrameworkConfigManager config)
{
trackManager = new TrackManager(new OnlineStore());
2018-05-08 19:55:48 +00:00
muteBindable = new BindableDouble();
2018-05-08 19:55:48 +00:00
audio.AddItem(trackManager);
config.BindWith(FrameworkSetting.VolumeMusic, trackManager.Volume);
2018-05-24 21:37:53 +00:00
onTrackStart = track =>
{
CurrentTrack?.Stop();
audio.Track.AddAdjustment(AdjustableProperty.Volume, muteBindable);
CurrentTrack = track;
CurrentTrack.Stopped += () => CurrentTrack = null;
};
onTrackStop = () => audio.Track.RemoveAdjustment(AdjustableProperty.Volume, muteBindable);
2018-05-08 19:55:48 +00:00
}
2018-05-24 21:37:53 +00:00
public PreviewTrack Get(BeatmapSetInfo beatmapSetInfo) =>
new PreviewTrack(
trackManager.Get($"https://b.ppy.sh/preview/{beatmapSetInfo.OnlineBeatmapSetID}.mp3"),
onTrackStart,
onTrackStop);
protected override void Update()
2018-05-08 19:55:48 +00:00
{
2018-05-24 21:37:53 +00:00
if (CurrentTrack?.Track.HasCompleted ?? false)
CurrentTrack.Stop();
2018-05-08 19:55:48 +00:00
base.Update();
2018-05-08 19:55:48 +00:00
}
}
}