1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-15 06:52:56 +08:00
osu-lazer/osu.Game/Audio/PreviewTrack.cs

110 lines
2.7 KiB
C#
Raw Normal View History

// 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-25 05:37:53 +08:00
using System;
using osu.Framework.Allocation;
2018-05-25 05:37:53 +08:00
using osu.Framework.Audio.Track;
using osu.Framework.Graphics;
using osu.Framework.Threading;
2018-05-25 05:37:53 +08:00
namespace osu.Game.Audio
{
[LongRunningLoad]
public abstract class PreviewTrack : Component
2018-05-25 05:37:53 +08:00
{
2018-06-21 17:54:42 +08:00
/// <summary>
/// Invoked when this <see cref="PreviewTrack"/> has stopped playing.
/// Not invoked in a thread-safe context.
2018-06-21 17:54:42 +08:00
/// </summary>
2018-05-25 05:37:53 +08:00
public event Action Stopped;
2018-06-21 17:54:42 +08:00
/// <summary>
/// Invoked when this <see cref="PreviewTrack"/> has started playing.
/// Not invoked in a thread-safe context.
2018-06-21 17:54:42 +08:00
/// </summary>
2018-05-25 05:37:53 +08:00
public event Action Started;
2019-11-12 14:13:47 +08:00
protected Track Track { get; private set; }
2018-06-22 11:12:59 +08:00
private bool hasStarted;
[BackgroundDependencyLoader]
private void load()
2018-05-25 05:37:53 +08:00
{
Track = GetTrack();
if (Track != null)
Track.Completed += Stop;
2018-05-25 05:37:53 +08: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 18:31:07 +08:00
/// <summary>
/// Whether the track is playing.
/// </summary>
public bool IsRunning => Track?.IsRunning ?? false;
2018-06-21 18:31:07 +08:00
private ScheduledDelegate startDelegate;
2018-06-21 17:54:42 +08:00
/// <summary>
/// Starts playing this <see cref="PreviewTrack"/>.
/// </summary>
/// <returns>Whether the track is started or already playing.</returns>
public bool Start()
2018-05-25 05:37:53 +08:00
{
if (Track == null)
return false;
startDelegate = Schedule(() =>
{
if (hasStarted)
return;
hasStarted = true;
Track.Restart();
Started?.Invoke();
});
return true;
}
2018-05-25 05:37:53 +08:00
2018-06-21 17:54:42 +08:00
/// <summary>
/// Stops playing this <see cref="PreviewTrack"/>.
/// </summary>
public void Stop()
2018-05-25 05:37:53 +08:00
{
startDelegate?.Cancel();
if (Track == null)
2018-06-03 03:06:45 +08:00
return;
2018-06-22 11:12:59 +08:00
if (!hasStarted)
return;
2018-06-22 11:12:59 +08:00
hasStarted = false;
Track.Stop();
2018-05-25 05:37:53 +08:00
Stopped?.Invoke();
}
2018-06-21 17:54:42 +08:00
/// <summary>
/// Retrieves the audio track.
/// </summary>
protected abstract Track GetTrack();
2018-05-25 05:37:53 +08:00
}
}