mirror of
https://github.com/ppy/osu
synced 2024-12-13 10:28:17 +00:00
113 lines
2.8 KiB
C#
113 lines
2.8 KiB
C#
// 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.Allocation;
|
|
using osu.Framework.Audio.Track;
|
|
using osu.Framework.Graphics;
|
|
using osu.Framework.Threading;
|
|
|
|
namespace osu.Game.Audio
|
|
{
|
|
public abstract class PreviewTrack : Component
|
|
{
|
|
/// <summary>
|
|
/// Invoked when this <see cref="PreviewTrack"/> has stopped playing.
|
|
/// </summary>
|
|
public event Action Stopped;
|
|
|
|
/// <summary>
|
|
/// Invoked when this <see cref="PreviewTrack"/> has started playing.
|
|
/// </summary>
|
|
public event Action Started;
|
|
|
|
private Track track;
|
|
private bool wasPlaying;
|
|
|
|
[BackgroundDependencyLoader]
|
|
private void load()
|
|
{
|
|
track = GetTrack();
|
|
|
|
if (track != null)
|
|
track.Looping = false;
|
|
}
|
|
|
|
/// <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;
|
|
|
|
/// <summary>
|
|
/// Whether the track is playing.
|
|
/// </summary>
|
|
public bool IsRunning => track?.IsRunning ?? false;
|
|
|
|
protected override void Update()
|
|
{
|
|
base.Update();
|
|
|
|
// Todo: Track currently doesn't signal its completion, so we have to handle it manually
|
|
if (track != null && wasPlaying && track.HasCompleted)
|
|
Stop();
|
|
}
|
|
|
|
private ScheduledDelegate startDelegate;
|
|
|
|
/// <summary>
|
|
/// Starts playing this <see cref="PreviewTrack"/>.
|
|
/// </summary>
|
|
public void Start() => startDelegate = Schedule(() =>
|
|
{
|
|
if (!IsLoaded)
|
|
return;
|
|
|
|
if (track == null)
|
|
return;
|
|
|
|
if (wasPlaying)
|
|
return;
|
|
wasPlaying = true;
|
|
|
|
track.Restart();
|
|
Started?.Invoke();
|
|
});
|
|
|
|
/// <summary>
|
|
/// Stops playing this <see cref="PreviewTrack"/>.
|
|
/// </summary>
|
|
public void Stop()
|
|
{
|
|
startDelegate?.Cancel();
|
|
|
|
if (!IsLoaded)
|
|
return;
|
|
|
|
if (track == null)
|
|
return;
|
|
|
|
if (!wasPlaying)
|
|
return;
|
|
wasPlaying = false;
|
|
|
|
track.Stop();
|
|
Stopped?.Invoke();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieves the audio track.
|
|
/// </summary>
|
|
protected abstract Track GetTrack();
|
|
}
|
|
}
|