2018-05-25 05:37:53 +08:00
|
|
|
|
// 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;
|
2018-06-02 04:36:25 +08:00
|
|
|
|
using osu.Framework.Allocation;
|
2018-05-25 05:37:53 +08:00
|
|
|
|
using osu.Framework.Audio.Track;
|
2018-06-02 04:36:25 +08:00
|
|
|
|
using osu.Framework.Graphics;
|
2018-06-21 15:19:07 +08:00
|
|
|
|
using osu.Framework.Threading;
|
2018-05-25 05:37:53 +08:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Audio
|
|
|
|
|
{
|
2018-06-21 15:19:07 +08:00
|
|
|
|
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.
|
|
|
|
|
/// </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.
|
|
|
|
|
/// </summary>
|
2018-05-25 05:37:53 +08:00
|
|
|
|
public event Action Started;
|
|
|
|
|
|
2018-06-21 15:19:07 +08:00
|
|
|
|
private Track track;
|
|
|
|
|
private bool wasPlaying;
|
|
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
|
private void load()
|
2018-05-25 05:37:53 +08:00
|
|
|
|
{
|
2018-06-21 15:19:07 +08:00
|
|
|
|
track = GetTrack();
|
|
|
|
|
|
|
|
|
|
if (track != null)
|
|
|
|
|
track.Looping = false;
|
2018-05-25 05:37:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-06-21 15:19:07 +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 15:19:07 +08:00
|
|
|
|
protected override void Update()
|
2018-06-02 04:36:25 +08:00
|
|
|
|
{
|
2018-06-21 15:19:07 +08:00
|
|
|
|
base.Update();
|
|
|
|
|
|
|
|
|
|
// Todo: Track currently doesn't signal its completion, so we have to handle it manually
|
|
|
|
|
if (track != null && wasPlaying && track.HasCompleted)
|
|
|
|
|
Stop();
|
2018-06-02 04:36:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-06-21 15:19:07 +08:00
|
|
|
|
private ScheduledDelegate startDelegate;
|
|
|
|
|
|
2018-06-21 17:54:42 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Starts playing this <see cref="PreviewTrack"/>.
|
|
|
|
|
/// </summary>
|
2018-06-21 15:19:07 +08:00
|
|
|
|
public void Start() => startDelegate = Schedule(() =>
|
2018-05-25 05:37:53 +08:00
|
|
|
|
{
|
2018-06-21 15:19:07 +08:00
|
|
|
|
if (!IsLoaded)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (track == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (wasPlaying)
|
|
|
|
|
return;
|
|
|
|
|
wasPlaying = true;
|
|
|
|
|
|
|
|
|
|
track.Restart();
|
2018-05-25 05:37:53 +08:00
|
|
|
|
Started?.Invoke();
|
2018-06-21 15:19:07 +08:00
|
|
|
|
});
|
2018-05-25 05:37:53 +08:00
|
|
|
|
|
2018-06-21 17:54:42 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Stops playing this <see cref="PreviewTrack"/>.
|
|
|
|
|
/// </summary>
|
2018-06-21 15:19:07 +08:00
|
|
|
|
public void Stop()
|
2018-05-25 05:37:53 +08:00
|
|
|
|
{
|
2018-06-21 15:19:07 +08:00
|
|
|
|
startDelegate?.Cancel();
|
|
|
|
|
|
|
|
|
|
if (!IsLoaded)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (track == null)
|
2018-06-03 03:06:45 +08:00
|
|
|
|
return;
|
2018-06-21 15:19:07 +08:00
|
|
|
|
|
|
|
|
|
if (!wasPlaying)
|
|
|
|
|
return;
|
|
|
|
|
wasPlaying = false;
|
|
|
|
|
|
|
|
|
|
track.Stop();
|
2018-05-25 05:37:53 +08:00
|
|
|
|
Stopped?.Invoke();
|
|
|
|
|
}
|
2018-06-21 15:19:07 +08:00
|
|
|
|
|
2018-06-21 17:54:42 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Retrieves the audio track.
|
|
|
|
|
/// </summary>
|
2018-06-21 15:19:07 +08:00
|
|
|
|
protected abstract Track GetTrack();
|
2018-05-25 05:37:53 +08:00
|
|
|
|
}
|
|
|
|
|
}
|