1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 15:33:05 +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 event Action PlaybackStarted;
public event Action PlaybackStopped;
private Action<PreviewTrack> onTrackStart;
private Action onTrackStop;
private TrackManager trackManager;
private BindableDouble muteBindable;
public Track CurrentTrack { get; private set; }
public PreviewTrack CurrentTrack { get; private set; }
[BackgroundDependencyLoader]
private void load(AudioManager audio, FrameworkConfigManager config)
@ -32,35 +32,28 @@ namespace osu.Game.Audio
audio.AddItem(trackManager);
config.BindWith(FrameworkSetting.VolumeMusic, trackManager.Volume);
PlaybackStarted += () => audio.Track.AddAdjustment(AdjustableProperty.Volume, muteBindable);
PlaybackStopped += () => audio.Track.RemoveAdjustment(AdjustableProperty.Volume, muteBindable);
onTrackStart = track =>
{
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()
{
if (CurrentTrack?.HasCompleted ?? false)
PlaybackStopped?.Invoke();
if (CurrentTrack?.Track.HasCompleted ?? false)
CurrentTrack.Stop();
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
using osu.Framework.Allocation;
using osu.Framework.Audio.Track;
using osu.Framework.Configuration;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Input;
using osu.Game.Audio;
using osu.Game.Beatmaps;
using osu.Game.Graphics;
using osu.Game.Graphics.Containers;
@ -25,7 +25,7 @@ namespace osu.Game.Overlays.BeatmapSet.Buttons
private readonly Box bg, progress;
private readonly PlayButton playButton;
private Track preview => playButton.Preview;
private PreviewTrack preview => playButton.Preview;
public Bindable<bool> Playing => playButton.Playing;
public BeatmapSetInfo BeatmapSet
@ -83,7 +83,7 @@ namespace osu.Game.Overlays.BeatmapSet.Buttons
if (Playing.Value && preview != null)
{
// 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
progress.Width = 0;

View File

@ -4,22 +4,22 @@
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Configuration;
using osu.Framework.Extensions.Color4Extensions;
using OpenTK;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Input;
using osu.Game.Audio;
using osu.Game.Beatmaps;
using osu.Game.Beatmaps.Drawables;
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
using OpenTK.Graphics;
using osu.Framework.Input;
using osu.Game.Graphics.UserInterface;
using osu.Game.Online.API.Requests;
using osu.Framework.Configuration;
using osu.Framework.Audio.Track;
using OpenTK;
using OpenTK.Graphics;
namespace osu.Game.Overlays.Direct
{
@ -37,7 +37,7 @@ namespace osu.Game.Overlays.Direct
private BeatmapManager beatmaps;
private BeatmapSetOverlay beatmapSetOverlay;
public Track Preview => PlayButton.Preview;
public PreviewTrack Preview => PlayButton.Preview;
public Bindable<bool> PreviewPlaying => PlayButton.Playing;
protected abstract PlayButton PlayButton { get; }
protected abstract Box PreviewBar { get; }
@ -121,9 +121,9 @@ namespace osu.Game.Overlays.Direct
{
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 osu.Framework.Allocation;
using osu.Framework.Audio.Track;
using osu.Framework.Configuration;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
@ -19,7 +18,7 @@ namespace osu.Game.Overlays.Direct
public class PlayButton : Container
{
public readonly Bindable<bool> Playing = new Bindable<bool>();
public Track Preview { get; private set; }
public PreviewTrack Preview { get; private set; }
private BeatmapSetInfo beatmapSet;
@ -32,6 +31,8 @@ namespace osu.Game.Overlays.Direct
beatmapSet = value;
Playing.Value = false;
if (Preview != null)
Preview.Stopped -= preview_Stopped;
Preview = null;
}
}
@ -85,12 +86,6 @@ namespace osu.Game.Overlays.Direct
{
hoverColour = colour.Yellow;
this.previewTrackManager = previewTrackManager;
previewTrackManager.PlaybackStopped += () =>
{
if (Preview == previewTrackManager.CurrentTrack)
Playing.Value = false;
};
}
protected override bool OnClick(InputState state)
@ -134,21 +129,24 @@ namespace osu.Game.Overlays.Direct
})
.ContinueWith(t =>
{
Preview.Stopped += preview_Stopped;
playingStateChanged(true);
loading = false;
});
return;
}
previewTrackManager.Play(Preview);
Preview.Start();
}
else
{
previewTrackManager.Stop();
Preview.Stop();
loading = false;
}
}
private void preview_Stopped() => Playing.Value = false;
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);