Skip to content

Commit

Permalink
Refactor sound player
Browse files Browse the repository at this point in the history
  • Loading branch information
i2van committed Sep 21, 2024
1 parent 5bbae16 commit 0cc4799
Showing 1 changed file with 10 additions and 26 deletions.
36 changes: 10 additions & 26 deletions Hourglass/Windows/SoundPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

using System.Windows;

namespace Hourglass.Windows;

using System;
Expand All @@ -20,7 +18,7 @@ namespace Hourglass.Windows;
/// <summary>
/// Plays <see cref="Sound"/>s stored in the file system.
/// </summary>
public class SoundPlayer : IDisposable
public sealed class SoundPlayer : IDisposable
{
#region Private Members

Expand All @@ -39,6 +37,8 @@ public class SoundPlayer : IDisposable
/// </summary>
private readonly MediaPlayer _mediaPlayer;

private bool _isLooping;

/// <summary>
/// Indicates whether this object has been disposed.
/// </summary>
Expand All @@ -62,29 +62,16 @@ public SoundPlayer()
_mediaPlayer.MediaEnded += MediaPlayerOnMediaEnded;
}

#region Events

/// <summary>
/// Raised when sound playback has completed.
/// </summary>
public event EventHandler? PlaybackCompleted;

#endregion

#region Properties

/// <summary>
/// Gets a value indicating whether the player is playing a sound.
/// </summary>
public bool IsPlaying { get; private set; }

/// <summary>
/// Gets a value indicating whether the player is looping the sound playback indefinitely.
/// </summary>
public bool IsLooping { get; private set; }

#endregion

#region Public Methods

/// <summary>
Expand All @@ -111,7 +98,7 @@ public void Play(Sound? sound, bool loop)
try
{
IsPlaying = true;
IsLooping = loop;
_isLooping = loop;

if (sound.IsBuiltIn)
{
Expand All @@ -138,16 +125,14 @@ public void Play(Sound? sound, bool loop)
}
else
{
MessageBox.Show($"Sound: {sound.Path!}", "Playing custom sound", MessageBoxButton.OK, MessageBoxImage.Information);

// Use the media player
_mediaPlayer.Open(new(sound.Path!));
_mediaPlayer.Play();
}
}
catch (Exception ex) when (ex.CanBeHandled())
{
MessageBox.Show($"Sound: {sound.Path!}\n{ex}", "Error playing sound", MessageBoxButton.OK, MessageBoxImage.Exclamation);
// Ignore.
}
}

Expand All @@ -163,7 +148,7 @@ public bool Stop()
try
{
IsPlaying = false;
IsLooping = false;
_isLooping = false;

// Stop the sound player
_soundPlayer.Stop();
Expand All @@ -190,7 +175,6 @@ public bool Stop()
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

#endregion
Expand All @@ -202,7 +186,7 @@ public void Dispose()
/// </summary>
/// <param name="disposing">A value indicating whether this method was invoked by an explicit call to <see
/// cref="Dispose"/>.</param>
protected virtual void Dispose(bool disposing)
private void Dispose(bool disposing)
{
if (_disposed)
{
Expand All @@ -214,7 +198,7 @@ protected virtual void Dispose(bool disposing)
if (disposing)
{
IsPlaying = false;
IsLooping = false;
_isLooping = false;

// Dispose the sound player
_soundPlayer.Stop();
Expand All @@ -232,7 +216,7 @@ protected virtual void Dispose(bool disposing)
/// <summary>
/// Throws a <see cref="ObjectDisposedException"/> if the object has been disposed.
/// </summary>
protected void ThrowIfDisposed()
private void ThrowIfDisposed()
{
if (_disposed)
{
Expand Down Expand Up @@ -262,7 +246,7 @@ private void DispatcherTimerTick(object sender, EventArgs e)
/// <param name="e">The event data.</param>
private void MediaPlayerOnMediaEnded(object sender, EventArgs e)
{
if (!IsLooping)
if (!_isLooping)
{
PlaybackCompleted?.Invoke(this, EventArgs.Empty);
return;
Expand Down

0 comments on commit 0cc4799

Please sign in to comment.