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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

114 lines
4.4 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-09 03:55:48 +08:00
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Audio.Track;
2019-02-21 18:04:31 +08:00
using osu.Framework.Bindables;
using osu.Framework.Graphics;
2018-05-09 03:55:48 +08:00
using osu.Framework.IO.Stores;
using osu.Framework.Logging;
using osu.Game.Beatmaps;
2018-05-09 03:55:48 +08:00
namespace osu.Game.Audio
{
public partial class PreviewTrackManager : Component
2018-05-09 03:55:48 +08:00
{
private readonly IAdjustableAudioComponent mainTrackAdjustments;
private readonly BindableDouble muteBindable = new BindableDouble();
private ITrackStore trackStore = null!;
protected TrackManagerPreviewTrack? CurrentTrack;
2018-05-09 03:55:48 +08:00
public PreviewTrackManager(IAdjustableAudioComponent mainTrackAdjustments)
{
this.mainTrackAdjustments = mainTrackAdjustments;
}
2018-05-09 03:55:48 +08:00
[BackgroundDependencyLoader]
2021-07-27 02:55:49 +08:00
private void load(AudioManager audioManager)
2018-05-09 03:55:48 +08:00
{
trackStore = audioManager.GetTrackStore(new OnlineStore());
}
2018-06-21 17:54:42 +08:00
/// <summary>
/// Retrieves a <see cref="PreviewTrack"/> for a <see cref="IBeatmapSetInfo"/>.
2018-06-21 17:54:42 +08:00
/// </summary>
/// <param name="beatmapSetInfo">The <see cref="IBeatmapSetInfo"/> to retrieve the preview track for.</param>
2018-06-21 17:54:42 +08:00
/// <returns>The playable <see cref="PreviewTrack"/>.</returns>
public PreviewTrack Get(IBeatmapSetInfo beatmapSetInfo)
{
var track = CreatePreviewTrack(beatmapSetInfo, trackStore);
2019-11-11 04:07:51 +08:00
track.Started += () => Schedule(() =>
2018-05-25 05:37:53 +08:00
{
CurrentTrack?.Stop();
CurrentTrack = track;
mainTrackAdjustments.AddAdjustment(AdjustableProperty.Volume, muteBindable);
2019-11-11 04:07:51 +08:00
});
track.Stopped += () => Schedule(() =>
{
if (CurrentTrack != track)
2019-11-11 04:07:51 +08:00
return;
CurrentTrack = null;
mainTrackAdjustments.RemoveAdjustment(AdjustableProperty.Volume, muteBindable);
});
2018-05-09 03:55:48 +08:00
return track;
}
2018-06-21 17:54:42 +08:00
/// <summary>
/// Stops any currently playing <see cref="PreviewTrack"/>.
2018-06-21 17:54:42 +08:00
/// </summary>
/// <remarks>
/// Only the immediate owner (an object that implements <see cref="IPreviewTrackOwner"/>) of the playing <see cref="PreviewTrack"/>
/// can globally stop the currently playing <see cref="PreviewTrack"/>. The object holding a reference to the <see cref="PreviewTrack"/>
/// can always stop the <see cref="PreviewTrack"/> themselves through <see cref="PreviewTrack.Stop()"/>.
/// </remarks>
/// <param name="source">The <see cref="IPreviewTrackOwner"/> which may be the owner of the <see cref="PreviewTrack"/>.</param>
public void StopAnyPlaying(IPreviewTrackOwner source)
{
if (CurrentTrack == null || (CurrentTrack.Owner != null && CurrentTrack.Owner != source))
return;
CurrentTrack.Stop();
2019-11-21 09:37:50 +08:00
// CurrentTrack should not be set to null here as it will result in incorrect handling in the track.Stopped callback above.
}
2018-06-21 18:31:07 +08:00
/// <summary>
/// Creates the <see cref="TrackManagerPreviewTrack"/>.
/// </summary>
protected virtual TrackManagerPreviewTrack CreatePreviewTrack(IBeatmapSetInfo beatmapSetInfo, ITrackStore trackStore) =>
new TrackManagerPreviewTrack(beatmapSetInfo, trackStore);
2018-06-21 18:31:07 +08:00
public partial class TrackManagerPreviewTrack : PreviewTrack
{
2022-08-01 19:45:15 +08:00
[Resolved]
public IPreviewTrackOwner? Owner { get; private set; }
private readonly IBeatmapSetInfo beatmapSetInfo;
2019-05-29 15:43:15 +08:00
private readonly ITrackStore trackManager;
public TrackManagerPreviewTrack(IBeatmapSetInfo beatmapSetInfo, ITrackStore trackManager)
{
this.beatmapSetInfo = beatmapSetInfo;
this.trackManager = trackManager;
}
protected override void LoadComplete()
{
base.LoadComplete();
if (Owner == null)
Logger.Log($"A {nameof(PreviewTrack)} was created without a containing {nameof(IPreviewTrackOwner)}. An owner should be added for correct behaviour.");
}
2021-10-26 10:57:51 +08:00
protected override Track GetTrack() => trackManager.Get($"https://b.ppy.sh/preview/{beatmapSetInfo.OnlineID}.mp3");
2018-05-09 03:55:48 +08:00
}
}
}