2019-01-24 16:43:03 +08: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-25 05:37:53 +08:00
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
{
|
2019-11-01 18:40:45 +08:00
|
|
|
|
[LongRunningLoad]
|
2022-11-24 13:32:20 +08:00
|
|
|
|
public abstract partial 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.
|
2019-11-06 14:58:47 +08:00
|
|
|
|
/// Not invoked in a thread-safe context.
|
2018-06-21 17:54:42 +08:00
|
|
|
|
/// </summary>
|
2022-07-31 22:00:14 +08:00
|
|
|
|
public event Action? Stopped;
|
2018-06-21 17:54:42 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Invoked when this <see cref="PreviewTrack"/> has started playing.
|
2019-11-06 14:58:47 +08:00
|
|
|
|
/// Not invoked in a thread-safe context.
|
2018-06-21 17:54:42 +08:00
|
|
|
|
/// </summary>
|
2022-07-31 22:00:14 +08:00
|
|
|
|
public event Action? Started;
|
2018-05-25 05:37:53 +08:00
|
|
|
|
|
2022-07-31 22:01:30 +08:00
|
|
|
|
protected Track? Track { get; private set; }
|
2019-11-11 04:09:04 +08:00
|
|
|
|
|
2018-06-22 11:12:59 +08:00
|
|
|
|
private bool hasStarted;
|
2018-06-21 15:19:07 +08:00
|
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
|
private void load()
|
2018-05-25 05:37:53 +08:00
|
|
|
|
{
|
2019-11-11 04:09:04 +08:00
|
|
|
|
Track = GetTrack();
|
|
|
|
|
if (Track != null)
|
|
|
|
|
Track.Completed += Stop;
|
2018-05-25 05:37:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-06-21 15:19:07 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Length of the track.
|
|
|
|
|
/// </summary>
|
2019-11-11 04:09:04 +08:00
|
|
|
|
public double Length => Track?.Length ?? 0;
|
2018-06-21 15:19:07 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The current track time.
|
|
|
|
|
/// </summary>
|
2019-11-11 04:09:04 +08:00
|
|
|
|
public double CurrentTime => Track?.CurrentTime ?? 0;
|
2018-06-21 15:19:07 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Whether the track is loaded.
|
|
|
|
|
/// </summary>
|
2019-11-11 04:09:04 +08:00
|
|
|
|
public bool TrackLoaded => Track?.IsLoaded ?? false;
|
2018-06-21 15:19:07 +08:00
|
|
|
|
|
2018-06-21 18:31:07 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Whether the track is playing.
|
|
|
|
|
/// </summary>
|
2019-11-11 04:09:04 +08:00
|
|
|
|
public bool IsRunning => Track?.IsRunning ?? false;
|
2018-06-21 18:31:07 +08:00
|
|
|
|
|
2022-07-31 22:01:30 +08:00
|
|
|
|
private ScheduledDelegate? startDelegate;
|
2018-06-21 15:19:07 +08:00
|
|
|
|
|
2018-06-21 17:54:42 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Starts playing this <see cref="PreviewTrack"/>.
|
|
|
|
|
/// </summary>
|
2019-05-23 11:07:22 +08:00
|
|
|
|
/// <returns>Whether the track is started or already playing.</returns>
|
|
|
|
|
public bool Start()
|
2018-05-25 05:37:53 +08:00
|
|
|
|
{
|
2019-11-11 04:09:04 +08:00
|
|
|
|
if (Track == null)
|
2019-05-23 11:07:22 +08:00
|
|
|
|
return false;
|
2018-06-21 15:19:07 +08:00
|
|
|
|
|
2019-05-23 11:07:22 +08:00
|
|
|
|
startDelegate = Schedule(() =>
|
|
|
|
|
{
|
|
|
|
|
if (hasStarted)
|
|
|
|
|
return;
|
2019-02-27 20:07:17 +08:00
|
|
|
|
|
2019-05-23 11:07:22 +08:00
|
|
|
|
hasStarted = true;
|
2018-06-21 15:19:07 +08:00
|
|
|
|
|
2019-11-11 04:09:04 +08:00
|
|
|
|
Track.Restart();
|
2019-05-23 11:07:22 +08:00
|
|
|
|
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>
|
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();
|
|
|
|
|
|
2019-11-11 04:09:04 +08:00
|
|
|
|
if (Track == null)
|
2018-06-03 03:06:45 +08:00
|
|
|
|
return;
|
2018-06-21 15:19:07 +08:00
|
|
|
|
|
2018-06-22 11:12:59 +08:00
|
|
|
|
if (!hasStarted)
|
2018-06-21 15:19:07 +08:00
|
|
|
|
return;
|
2019-02-27 20:07:17 +08:00
|
|
|
|
|
2018-06-22 11:12:59 +08:00
|
|
|
|
hasStarted = false;
|
2018-06-21 15:19:07 +08:00
|
|
|
|
|
2023-12-26 11:06:56 +08:00
|
|
|
|
// This pre-check is important, fixes a BASS deadlock in some scenarios.
|
2023-12-26 02:01:06 +08:00
|
|
|
|
if (!Track.HasCompleted)
|
|
|
|
|
{
|
|
|
|
|
Track.Stop();
|
2019-11-06 14:58:47 +08:00
|
|
|
|
|
2023-12-26 02:01:06 +08:00
|
|
|
|
// Ensure the track is reset immediately on stopping, so the next time it is started it has a correct time value.
|
|
|
|
|
Track.Seek(0);
|
|
|
|
|
}
|
2023-02-14 14:39:34 +08:00
|
|
|
|
|
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>
|
2022-07-31 22:01:30 +08:00
|
|
|
|
protected abstract Track? GetTrack();
|
2022-08-05 13:15:01 +08:00
|
|
|
|
|
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
|
{
|
|
|
|
|
base.Dispose(isDisposing);
|
2022-12-05 19:45:29 +08:00
|
|
|
|
|
|
|
|
|
Stop();
|
2022-08-05 13:15:01 +08:00
|
|
|
|
Track?.Dispose();
|
|
|
|
|
}
|
2018-05-25 05:37:53 +08:00
|
|
|
|
}
|
|
|
|
|
}
|