2019-01-24 08:43:03 +00:00
|
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
2018-05-24 21:37:53 +00:00
|
|
|
|
|
|
|
|
|
using System;
|
2018-06-01 20:36:25 +00:00
|
|
|
|
using osu.Framework.Allocation;
|
2018-05-24 21:37:53 +00:00
|
|
|
|
using osu.Framework.Audio.Track;
|
2018-06-01 20:36:25 +00:00
|
|
|
|
using osu.Framework.Graphics;
|
2018-06-21 07:19:07 +00:00
|
|
|
|
using osu.Framework.Threading;
|
2018-05-24 21:37:53 +00:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Audio
|
|
|
|
|
{
|
2018-06-21 07:19:07 +00:00
|
|
|
|
public abstract class PreviewTrack : Component
|
2018-05-24 21:37:53 +00:00
|
|
|
|
{
|
2018-06-21 09:54:42 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Invoked when this <see cref="PreviewTrack"/> has stopped playing.
|
|
|
|
|
/// </summary>
|
2018-05-24 21:37:53 +00:00
|
|
|
|
public event Action Stopped;
|
2018-06-21 09:54:42 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Invoked when this <see cref="PreviewTrack"/> has started playing.
|
|
|
|
|
/// </summary>
|
2018-05-24 21:37:53 +00:00
|
|
|
|
public event Action Started;
|
|
|
|
|
|
2018-06-21 07:19:07 +00:00
|
|
|
|
private Track track;
|
2018-06-22 03:12:59 +00:00
|
|
|
|
private bool hasStarted;
|
2018-06-21 07:19:07 +00:00
|
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
|
private void load()
|
2018-05-24 21:37:53 +00:00
|
|
|
|
{
|
2018-06-21 07:19:07 +00:00
|
|
|
|
track = GetTrack();
|
2019-02-27 07:52:34 +00:00
|
|
|
|
track.Completed += () => Schedule(Stop);
|
2018-05-24 21:37:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-06-21 07:19:07 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Length of the track.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public double Length => track?.Length ?? 0;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The current track time.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public double CurrentTime => track?.CurrentTime ?? 0;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Whether the track is loaded.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool TrackLoaded => track?.IsLoaded ?? false;
|
|
|
|
|
|
2018-06-21 10:31:07 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Whether the track is playing.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool IsRunning => track?.IsRunning ?? false;
|
|
|
|
|
|
2018-06-21 07:19:07 +00:00
|
|
|
|
private ScheduledDelegate startDelegate;
|
|
|
|
|
|
2018-06-21 09:54:42 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Starts playing this <see cref="PreviewTrack"/>.
|
|
|
|
|
/// </summary>
|
2018-06-21 07:19:07 +00:00
|
|
|
|
public void Start() => startDelegate = Schedule(() =>
|
2018-05-24 21:37:53 +00:00
|
|
|
|
{
|
2018-06-21 07:19:07 +00:00
|
|
|
|
if (track == null)
|
|
|
|
|
return;
|
|
|
|
|
|
2018-06-22 03:12:59 +00:00
|
|
|
|
if (hasStarted)
|
2018-06-21 07:19:07 +00:00
|
|
|
|
return;
|
2019-02-27 12:07:17 +00:00
|
|
|
|
|
2018-06-22 03:12:59 +00:00
|
|
|
|
hasStarted = true;
|
2018-06-21 07:19:07 +00:00
|
|
|
|
|
|
|
|
|
track.Restart();
|
2018-05-24 21:37:53 +00:00
|
|
|
|
Started?.Invoke();
|
2018-06-21 07:19:07 +00:00
|
|
|
|
});
|
2018-05-24 21:37:53 +00:00
|
|
|
|
|
2018-06-21 09:54:42 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Stops playing this <see cref="PreviewTrack"/>.
|
|
|
|
|
/// </summary>
|
2018-06-21 07:19:07 +00:00
|
|
|
|
public void Stop()
|
2018-05-24 21:37:53 +00:00
|
|
|
|
{
|
2018-06-21 07:19:07 +00:00
|
|
|
|
startDelegate?.Cancel();
|
|
|
|
|
|
|
|
|
|
if (track == null)
|
2018-06-02 19:06:45 +00:00
|
|
|
|
return;
|
2018-06-21 07:19:07 +00:00
|
|
|
|
|
2018-06-22 03:12:59 +00:00
|
|
|
|
if (!hasStarted)
|
2018-06-21 07:19:07 +00:00
|
|
|
|
return;
|
2019-02-27 12:07:17 +00:00
|
|
|
|
|
2018-06-22 03:12:59 +00:00
|
|
|
|
hasStarted = false;
|
2018-06-21 07:19:07 +00:00
|
|
|
|
|
|
|
|
|
track.Stop();
|
2018-05-24 21:37:53 +00:00
|
|
|
|
Stopped?.Invoke();
|
|
|
|
|
}
|
2018-06-21 07:19:07 +00:00
|
|
|
|
|
2018-06-21 09:54:42 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Retrieves the audio track.
|
|
|
|
|
/// </summary>
|
2018-06-21 07:19:07 +00:00
|
|
|
|
protected abstract Track GetTrack();
|
2018-05-24 21:37:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|