Skip to content

Commit

Permalink
Merge pull request #59 from UTheCat/music-player-code-revamp
Browse files Browse the repository at this point in the history
Improve MusicPlayer's code
  • Loading branch information
UTheCat authored Jul 23, 2024
2 parents 201043c + 52e5619 commit a18663e
Showing 1 changed file with 47 additions and 14 deletions.
61 changes: 47 additions & 14 deletions src/core/Music/MusicPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ private void StopPlaylist(Playlist playlist, bool immediateStop = false)
if (immediateStop)
{
playlist.StopImmediately();
RemovePlaylistInternal(playlist);
//RemovePlaylistInternal(playlist);
}
else
{
playlist.Stopped += HandlePlaylistStop;
//playlist.Stopped += HandlePlaylistStop;
fadingOutPlaylists.Add(playlist);
playlist.Stop();
}
Expand Down Expand Up @@ -125,10 +125,12 @@ protected set
// connect to the new playlist's SongChanged event
value.SongChanged += HandlePlaylistSongChange;

/*
if (!value.IsInsideTree())
{
AddChild(value);
}
*/

// in case this playlist wanting to be played is currently fading out, stop it from being removed
if (fadingOutPlaylists.Contains(value))
Expand All @@ -140,15 +142,6 @@ protected set
}

// play the new playlist (this is where MusicPlayer.SongChanged will get raised for the song change)
if (OverrideTransitionTime)
{
value.TransitionTime = TransitionTime;
}
if (OverrideLocalVolumeScale)
{
value.LocalVolumeScale = VolumeScale;
}

value.Play();
}
}
Expand Down Expand Up @@ -263,9 +256,39 @@ public bool IsPlaying
}
}

/// <summary>
/// Whether or not this <see cref="MusicPlayer"/> should add/remove this music player
/// as a parent when the <see cref="AddPlaylist"/> and <see cref="RemovePlaylist"/>
/// methods are called.
/// </summary>
public bool ShouldSetPlaylistParent;

public MusicPlayer()
{
Playlists = new List<Playlist>();
ShouldSetPlaylistParent = false;
}

/// <summary>
/// Method that can be overriden to define how playlists added to this music player
/// should have their properties overriden.
/// By default, overrides defined in the <see cref="MusicPlayer"/> class are applied here.
/// </summary>
/// <param name="playlist">The playlist to apply overrides to</param>
public virtual void ApplyOverrides(Playlist playlist)
{
if (OverrideTransitionTime)
{
playlist.TransitionTime = TransitionTime;
}
if (OverrideLocalVolumeScale)
{
playlist.LocalVolumeScale = VolumeScale;
}
if (OverrideSongStreamHandlingMode)
{
playlist.SongStreamHandlingMode = SongStreamHandlingMode;
}
}

/// <summary>
Expand All @@ -276,7 +299,15 @@ public MusicPlayer()
/// <param name="playlist"></param>
public void RemovePlaylist(Playlist playlist)
{
if (Playlists.Contains(playlist)) Playlists.Remove(playlist);
if (Playlists.Contains(playlist))
{
if (ShouldSetPlaylistParent == true && playlist.GetParent() == this)
{
RemoveChild(playlist);
}

Playlists.Remove(playlist);
}
}

/// <summary>
Expand All @@ -291,9 +322,11 @@ public void AddPlaylist(Playlist playlist)

Playlists.Add(playlist);

if (OverrideSongStreamHandlingMode)
ApplyOverrides(playlist);

if (ShouldSetPlaylistParent == true && playlist.GetParent() == null)
{
playlist.SongStreamHandlingMode = SongStreamHandlingMode;
AddChild(playlist);
}
}

Expand Down

0 comments on commit a18663e

Please sign in to comment.