// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using osu.Framework.Allocation; using osu.Framework.Audio; using osu.Framework.Audio.Track; using osu.Framework.Bindables; using osu.Framework.Configuration; using osu.Framework.Graphics; using osu.Framework.IO.Stores; using osu.Game.Beatmaps; namespace osu.Game.Audio { /// /// A central store for the retrieval of s. /// public class PreviewTrackManager : Component { private readonly BindableDouble muteBindable = new BindableDouble(); private AudioManager audio; private ITrackStore trackStore; private TrackManagerPreviewTrack current; [BackgroundDependencyLoader] private void load(AudioManager audio, FrameworkConfigManager config) { trackStore = audio.GetTrackStore(new OnlineStore()); this.audio = audio; config.BindWith(FrameworkSetting.VolumeMusic, trackStore.Volume); } /// /// Retrieves a for a . /// /// The to retrieve the preview track for. /// The playable . public PreviewTrack Get(BeatmapSetInfo beatmapSetInfo) { var track = CreatePreviewTrack(beatmapSetInfo, trackStore); track.Started += () => { current?.Stop(); current = track; audio.Tracks.AddAdjustment(AdjustableProperty.Volume, muteBindable); }; track.Stopped += () => { current = null; audio.Tracks.RemoveAdjustment(AdjustableProperty.Volume, muteBindable); }; return track; } /// /// Stops any currently playing . /// /// /// Only the immediate owner (an object that implements ) of the playing /// can globally stop the currently playing . The object holding a reference to the /// can always stop the themselves through . /// /// The which may be the owner of the . public void StopAnyPlaying(IPreviewTrackOwner source) { if (current == null || current.Owner != source) return; current.Stop(); current = null; } /// /// Creates the . /// protected virtual TrackManagerPreviewTrack CreatePreviewTrack(BeatmapSetInfo beatmapSetInfo, ITrackStore trackStore) => new TrackManagerPreviewTrack(beatmapSetInfo, trackStore); protected class TrackManagerPreviewTrack : PreviewTrack { public IPreviewTrackOwner Owner { get; private set; } private readonly BeatmapSetInfo beatmapSetInfo; private readonly ITrackStore trackManager; public TrackManagerPreviewTrack(BeatmapSetInfo beatmapSetInfo, ITrackStore trackManager) { this.beatmapSetInfo = beatmapSetInfo; this.trackManager = trackManager; } [BackgroundDependencyLoader] private void load(IPreviewTrackOwner owner) { Owner = owner; } protected override Track GetTrack() => trackManager.Get($"https://b.ppy.sh/preview/{beatmapSetInfo?.OnlineBeatmapSetID}.mp3"); } } }