Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 0 additions & 19 deletions src/model/IPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace MusicSharp
{
using NAudio.Wave;

/// <summary>
/// Defines the methods an audio player class should implement.
/// </summary>
Expand All @@ -16,16 +14,6 @@ public interface IPlayer
/// </summary>
string LastFileOpened { get; set; }

/// <summary>
/// Gets or sets the audio output device.
/// </summary>
WaveOutEvent OutputDevice { get; set; }

/// <summary>
/// Gets or sets the audio file to load.
/// </summary>
AudioFileReader AudioFile { get; set; }

/// <summary>
/// Method to play audio.
/// </summary>
Expand All @@ -41,12 +29,5 @@ public interface IPlayer
/// Method to stop audio playback.
/// </summary>
void Stop();

/// <summary>
/// Method to dispose of resources when audio playback is finished.
/// </summary>
/// <param name="sender">The object.</param>
/// <param name="args">StoppedEventArgs.</param>
void OnPlaybackStopped(object sender, StoppedEventArgs args);
}
}
95 changes: 14 additions & 81 deletions src/model/WinPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,116 +11,49 @@ namespace MusicSharp
/// </summary>
public class WinPlayer : IPlayer
{
private WaveOutEvent outputDevice;
private AudioFileReader audioFile;
private readonly WaveOutEvent outputDevice;
private AudioFileReader audioFileReader;

/// <inheritdoc/>
public string LastFileOpened { get; set; }

/// <inheritdoc/>
public WaveOutEvent OutputDevice
{
get
{
return this.outputDevice;
}

set
{
this.outputDevice = this.OutputDevice;
}
}

/// <inheritdoc/>
public AudioFileReader AudioFile
public WinPlayer()
{
get
{
return this.audioFile;
}

set
{
this.audioFile = this.AudioFile;
}
this.outputDevice = new WaveOutEvent();
this.outputDevice.PlaybackStopped += this.OnPlaybackStopped;
}

/// <inheritdoc/>
public void Stop()
{
if (this.outputDevice != null)
{
try
{
this.outputDevice?.Stop();
this.outputDevice.PlaybackStopped += this.OnPlaybackStopped;
}
catch (System.NullReferenceException)
{
}
}
this.outputDevice.Stop();
}

/// <inheritdoc/>
public void Play(string path)
{
if (this.outputDevice == null)
if (this.outputDevice.PlaybackState != PlaybackState.Stopped)
{
this.outputDevice = new WaveOutEvent();
this.outputDevice.PlaybackStopped += this.OnPlaybackStopped;
return;
}

if (this.audioFile == null)
{
try
{
this.audioFile = new AudioFileReader(this.LastFileOpened);
this.outputDevice.Init(this.audioFile);
this.outputDevice.Play();
}
catch (System.Runtime.InteropServices.COMException)
{
}
}

if (this.audioFile != null)
{
try
{
this.outputDevice.Play();
}
catch (System.Runtime.InteropServices.COMException)
{
}
}
this.audioFileReader = new AudioFileReader(path);
this.outputDevice.Init(this.audioFileReader);
this.outputDevice.Play();
}

/// <inheritdoc/>
public void Pause()
{
try
{
this.outputDevice?.Pause();
}
catch (System.NullReferenceException)
{
}
this.outputDevice.Pause();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Testing this PR, it seems that the pause button isn't working. After some review, it appears that lines 40-41 inside the Player method are creating a new instance of audioFileReader and outputDevice each time the Play button is pressed, which resets the pause method.

My solution was to move the initialization inside an if statement inside the Play method like so:

public void Play(string path)
{
if (this.outputDevice.PlaybackState == PlaybackState.Stopped)
{
this.audioFileReader = new AudioFileReader(path);
this.outputDevice.Init(this.audioFileReader);
}
this.outputDevice.Play();
}

}

/// <inheritdoc/>
// Dispose of our device and AudioFile once playback is stopped.
// These will be changed in the future as we might want to allow
// users to carry on playback from where they left off.
// Dispose of our device once playback is stopped.
public void OnPlaybackStopped(object sender, StoppedEventArgs args)
{
this.audioFileReader.Dispose();
this.outputDevice.Dispose();
this.outputDevice = null;

// this.AudioFile.Dispose();

// By resetting the AudioFile position to 0, playback can start again.
// this.AudioFile.Position = 0;
// this.AudioFile = null;
}
}
}
18 changes: 5 additions & 13 deletions src/view/Gui.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,12 @@ public void Start()
var playBtn = new Button(1, 1, "Play");
playBtn.Clicked += () =>
{
if (this.player.LastFileOpened != null && this.player.OutputDevice != null)
{
try
{
this.player.OutputDevice.Play();
}
catch (System.NullReferenceException)
{
}
}
else
{
if(this.player.LastFileOpened == null) {
this.OpenFile();
return;
}

this.player.Play(this.player.LastFileOpened);
};

var pauseBtn = new Button(10, 1, "Pause");
Expand Down Expand Up @@ -169,7 +161,7 @@ private void OpenFile()
if (!d.Canceled)
{
this.player.LastFileOpened = d.FilePath.ToString();
this.player.Play(d.FilePath.ToString());
this.player.Play(this.player.LastFileOpened);
}
}
}
Expand Down