1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-15 05:02:55 +08:00

Introduce PreviewTrack class

This commit is contained in:
Roman Kapustin 2018-05-25 00:37:53 +03:00
parent d6076eb591
commit 498244a308
5 changed files with 76 additions and 46 deletions

View File

@ -0,0 +1,39 @@
// 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;
using osu.Framework.Audio.Track;
namespace osu.Game.Audio
{
public class PreviewTrack
{
public readonly Track Track;
private readonly Action<PreviewTrack> onStart;
private readonly Action onStop;
public event Action Stopped;
public event Action Started;
public PreviewTrack(Track track, Action<PreviewTrack> onStart, Action onStop)
{
Track = track;
this.onStart = onStart;
this.onStop = onStop;
}
public void Start()
{
onStart?.Invoke(this);
Track.Restart();
Started?.Invoke();
}
public void Stop()
{
onStop?.Invoke();
Track.Stop();
Stopped?.Invoke();
}
}
}

View File

@ -14,13 +14,13 @@ namespace osu.Game.Audio
{ {
public class PreviewTrackManager : Component public class PreviewTrackManager : Component
{ {
public event Action PlaybackStarted; private Action<PreviewTrack> onTrackStart;
public event Action PlaybackStopped; private Action onTrackStop;
private TrackManager trackManager; private TrackManager trackManager;
private BindableDouble muteBindable; private BindableDouble muteBindable;
public Track CurrentTrack { get; private set; } public PreviewTrack CurrentTrack { get; private set; }
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load(AudioManager audio, FrameworkConfigManager config) private void load(AudioManager audio, FrameworkConfigManager config)
@ -32,35 +32,28 @@ namespace osu.Game.Audio
audio.AddItem(trackManager); audio.AddItem(trackManager);
config.BindWith(FrameworkSetting.VolumeMusic, trackManager.Volume); config.BindWith(FrameworkSetting.VolumeMusic, trackManager.Volume);
PlaybackStarted += () => audio.Track.AddAdjustment(AdjustableProperty.Volume, muteBindable); onTrackStart = track =>
PlaybackStopped += () => audio.Track.RemoveAdjustment(AdjustableProperty.Volume, muteBindable); {
CurrentTrack?.Stop();
audio.Track.AddAdjustment(AdjustableProperty.Volume, muteBindable);
CurrentTrack = track;
CurrentTrack.Stopped += () => CurrentTrack = null;
};
onTrackStop = () => audio.Track.RemoveAdjustment(AdjustableProperty.Volume, muteBindable);
} }
public Track Get(BeatmapSetInfo beatmapSetInfo) => trackManager.Get($"https://b.ppy.sh/preview/{beatmapSetInfo.OnlineBeatmapSetID}.mp3"); public PreviewTrack Get(BeatmapSetInfo beatmapSetInfo) =>
new PreviewTrack(
trackManager.Get($"https://b.ppy.sh/preview/{beatmapSetInfo.OnlineBeatmapSetID}.mp3"),
onTrackStart,
onTrackStop);
protected override void Update() protected override void Update()
{ {
if (CurrentTrack?.HasCompleted ?? false) if (CurrentTrack?.Track.HasCompleted ?? false)
PlaybackStopped?.Invoke(); CurrentTrack.Stop();
base.Update(); base.Update();
} }
public void Play(Track track)
{
Stop();
CurrentTrack = track;
track.Restart();
PlaybackStarted?.Invoke();
}
public void Stop()
{
if (CurrentTrack?.IsRunning ?? false)
{
CurrentTrack?.Stop();
PlaybackStopped?.Invoke();
}
}
} }
} }

View File

@ -2,13 +2,13 @@
// 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 osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Audio.Track;
using osu.Framework.Configuration; using osu.Framework.Configuration;
using osu.Framework.Extensions.Color4Extensions; using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes; using osu.Framework.Graphics.Shapes;
using osu.Framework.Input; using osu.Framework.Input;
using osu.Game.Audio;
using osu.Game.Beatmaps; using osu.Game.Beatmaps;
using osu.Game.Graphics; using osu.Game.Graphics;
using osu.Game.Graphics.Containers; using osu.Game.Graphics.Containers;
@ -25,7 +25,7 @@ namespace osu.Game.Overlays.BeatmapSet.Buttons
private readonly Box bg, progress; private readonly Box bg, progress;
private readonly PlayButton playButton; private readonly PlayButton playButton;
private Track preview => playButton.Preview; private PreviewTrack preview => playButton.Preview;
public Bindable<bool> Playing => playButton.Playing; public Bindable<bool> Playing => playButton.Playing;
public BeatmapSetInfo BeatmapSet public BeatmapSetInfo BeatmapSet
@ -83,7 +83,7 @@ namespace osu.Game.Overlays.BeatmapSet.Buttons
if (Playing.Value && preview != null) if (Playing.Value && preview != null)
{ {
// prevent negative (potential infinite) width if a track without length was loaded // prevent negative (potential infinite) width if a track without length was loaded
progress.Width = preview.Length > 0 ? (float)(preview.CurrentTime / preview.Length) : 0f; progress.Width = preview.Track.Length > 0 ? (float)(preview.Track.CurrentTime / preview.Track.Length) : 0f;
} }
else else
progress.Width = 0; progress.Width = 0;

View File

@ -4,22 +4,22 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Configuration;
using osu.Framework.Extensions.Color4Extensions; using osu.Framework.Extensions.Color4Extensions;
using OpenTK;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes; using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Sprites; using osu.Framework.Graphics.Sprites;
using osu.Framework.Input;
using osu.Game.Audio;
using osu.Game.Beatmaps; using osu.Game.Beatmaps;
using osu.Game.Beatmaps.Drawables; using osu.Game.Beatmaps.Drawables;
using osu.Game.Graphics; using osu.Game.Graphics;
using osu.Game.Graphics.Sprites; using osu.Game.Graphics.Sprites;
using OpenTK.Graphics;
using osu.Framework.Input;
using osu.Game.Graphics.UserInterface; using osu.Game.Graphics.UserInterface;
using osu.Game.Online.API.Requests; using osu.Game.Online.API.Requests;
using osu.Framework.Configuration; using OpenTK;
using osu.Framework.Audio.Track; using OpenTK.Graphics;
namespace osu.Game.Overlays.Direct namespace osu.Game.Overlays.Direct
{ {
@ -37,7 +37,7 @@ namespace osu.Game.Overlays.Direct
private BeatmapManager beatmaps; private BeatmapManager beatmaps;
private BeatmapSetOverlay beatmapSetOverlay; private BeatmapSetOverlay beatmapSetOverlay;
public Track Preview => PlayButton.Preview; public PreviewTrack Preview => PlayButton.Preview;
public Bindable<bool> PreviewPlaying => PlayButton.Playing; public Bindable<bool> PreviewPlaying => PlayButton.Playing;
protected abstract PlayButton PlayButton { get; } protected abstract PlayButton PlayButton { get; }
protected abstract Box PreviewBar { get; } protected abstract Box PreviewBar { get; }
@ -121,9 +121,9 @@ namespace osu.Game.Overlays.Direct
{ {
base.Update(); base.Update();
if (PreviewPlaying && Preview != null && Preview.IsLoaded) if (PreviewPlaying && Preview != null && Preview.Track.IsLoaded)
{ {
PreviewBar.Width = (float)(Preview.CurrentTime / Preview.Length); PreviewBar.Width = (float)(Preview.Track.CurrentTime / Preview.Track.Length);
} }
} }

View File

@ -3,7 +3,6 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Audio.Track;
using osu.Framework.Configuration; using osu.Framework.Configuration;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
@ -19,7 +18,7 @@ namespace osu.Game.Overlays.Direct
public class PlayButton : Container public class PlayButton : Container
{ {
public readonly Bindable<bool> Playing = new Bindable<bool>(); public readonly Bindable<bool> Playing = new Bindable<bool>();
public Track Preview { get; private set; } public PreviewTrack Preview { get; private set; }
private BeatmapSetInfo beatmapSet; private BeatmapSetInfo beatmapSet;
@ -32,6 +31,8 @@ namespace osu.Game.Overlays.Direct
beatmapSet = value; beatmapSet = value;
Playing.Value = false; Playing.Value = false;
if (Preview != null)
Preview.Stopped -= preview_Stopped;
Preview = null; Preview = null;
} }
} }
@ -85,12 +86,6 @@ namespace osu.Game.Overlays.Direct
{ {
hoverColour = colour.Yellow; hoverColour = colour.Yellow;
this.previewTrackManager = previewTrackManager; this.previewTrackManager = previewTrackManager;
previewTrackManager.PlaybackStopped += () =>
{
if (Preview == previewTrackManager.CurrentTrack)
Playing.Value = false;
};
} }
protected override bool OnClick(InputState state) protected override bool OnClick(InputState state)
@ -134,21 +129,24 @@ namespace osu.Game.Overlays.Direct
}) })
.ContinueWith(t => .ContinueWith(t =>
{ {
Preview.Stopped += preview_Stopped;
playingStateChanged(true); playingStateChanged(true);
loading = false; loading = false;
}); });
return; return;
} }
previewTrackManager.Play(Preview); Preview.Start();
} }
else else
{ {
previewTrackManager.Stop(); Preview.Stop();
loading = false; loading = false;
} }
} }
private void preview_Stopped() => Playing.Value = false;
protected override void Dispose(bool isDisposing) protected override void Dispose(bool isDisposing)
{ {
base.Dispose(isDisposing); base.Dispose(isDisposing);