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
16 changes: 8 additions & 8 deletions src/model/IPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,18 @@ public interface IPlayer
/// Method to play audio.
/// </summary>
/// <param name="path">The filepath of the audio file to play.</param>
void Play(string path);
void OpenFile(string path);

/// <summary>
/// Method to play an audio stream from a URL.
/// </summary>
/// <param name="streamURL">The stream URL of the audio file to play.</param>
void OpenStream(string streamURL);

/// <summary>
/// Method to pause audio playback.
/// </summary>
void Pause();
void PlayPause();

/// <summary>
/// Method to stop audio playback.
Expand All @@ -39,11 +45,5 @@ public interface IPlayer
/// Method to decrease audio playback volume.
/// </summary>
void DecreaseVolume();

/// <summary>
/// Method to play an audio stream from a URL.
/// </summary>
/// <param name="streamURL">The stream URL of the audio file to play.</param>
void PlayStream(string streamURL);
}
}
28 changes: 18 additions & 10 deletions src/model/WinPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,29 @@ public void Stop()
}

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

this.audioFileReader = new AudioFileReader(path);
this.outputDevice.Init(this.audioFileReader);
this.outputDevice.Play();
}

/// <inheritdoc/>
public void Pause()
public void PlayPause()
{
this.outputDevice.Pause();
if (
this.outputDevice.PlaybackState == PlaybackState.Paused ||
this.outputDevice.PlaybackState == PlaybackState.Stopped
)
{
this.outputDevice.Play();
return;
}

if (this.outputDevice.PlaybackState == PlaybackState.Playing)
{
this.outputDevice.Pause();
}
}

/// <summary>
Expand Down Expand Up @@ -98,7 +106,7 @@ public void DecreaseVolume()
}

/// <inheritdoc/>
public void PlayStream(string streamURL)
public void OpenStream(string streamURL)
{
try
{
Expand Down
50 changes: 26 additions & 24 deletions src/view/Tui.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,11 @@ public void Start()

statusBar = new StatusBar(new StatusItem[]
{
new StatusItem(Key.F1, "~F1~ Open file", () => this.OpenFile()),
new StatusItem(Key.F2, "~F2~ Open stream", () => this.OpenStream()),
new StatusItem(Key.F3, "~F3~ Load playlist", () => this.LoadPlaylist()),
new StatusItem(Key.F4, "~F4~ Quit", () => Application.RequestStop()),
new StatusItem(Key.F1, "~F1~ Open file", () => this.OpenFile()),
new StatusItem(Key.F2, "~F2~ Open stream", () => this.OpenStream()),
new StatusItem(Key.F3, "~F3~ Load playlist", () => this.LoadPlaylist()),
new StatusItem(Key.F4, "~F4~ Quit", () => Application.RequestStop()),
new StatusItem(Key.Space, "~Space~ Play/Pause", () => this.PlayPause()),
});

// Create the playback controls frame.
Expand All @@ -100,26 +101,14 @@ public void Start()
CanFocus = true,
};

var playBtn = new Button(1, 1, "Play");
playBtn.Clicked += () =>
var playPauseButton = new Button(1, 1, "Play/Pause");
playPauseButton.Clicked += () =>
{
if (this.player.LastFileOpened == null)
{
this.OpenFile();
return;
}

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

var pauseBtn = new Button(10, 1, "Pause");
pauseBtn.Clicked += () =>
{
this.player.Pause();
};

var stopBtn = new Button(20, 1, "Stop");
stopBtn.Clicked += () =>
var stopButton = new Button(16, 1, "Stop");
stopButton.Clicked += () =>
{
this.player.Stop();
};
Expand All @@ -136,7 +125,7 @@ public void Start()
this.player.DecreaseVolume();
};

playbackControls.Add(playBtn, pauseBtn, stopBtn, increaseVolumeButton, decreaseVolumeButton);
playbackControls.Add(playPauseButton, stopButton, increaseVolumeButton, decreaseVolumeButton);

// Create the left-hand playlists view.
leftPane = new FrameView("Artists")
Expand Down Expand Up @@ -172,6 +161,7 @@ public void Start()
X = 25,
Y = 1, // for menu
Width = Dim.Fill(),

Height = 23,
CanFocus = true,
};
Expand Down Expand Up @@ -214,6 +204,18 @@ public void Start()
Application.Run();
}

private void PlayPause()
{
try
{
this.player.PlayPause();
}
catch(Exception)
{
MessageBox.Query("Warning", "Select a file or stream first.", "Close");
}
}

// Display a file open dialog and return the path of the user selected file.
private void OpenFile()
{
Expand All @@ -228,7 +230,7 @@ private void OpenFile()
if (!d.Canceled)
{
this.player.LastFileOpened = d.FilePath.ToString();
this.player.Play(this.player.LastFileOpened);
this.player.OpenFile(this.player.LastFileOpened);
}
}

Expand All @@ -253,7 +255,7 @@ private void OpenStream()
var loadStream = new Button(12, 7, "Load Stream");
loadStream.Clicked += () =>
{
this.player.PlayStream(streamURL.Text.ToString());
this.player.OpenStream(streamURL.Text.ToString());
Application.RequestStop();
};

Expand Down
2 changes: 1 addition & 1 deletion tests/MusicSharpTests/src/WinPlayerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public void Play_NullFile()
var player = new WinPlayer();

// act and assert
Assert.ThrowsException<System.NullReferenceException>(() => player.Play(null));
Assert.ThrowsException<System.NullReferenceException>(() => player.OpenFile(null));
}
}
}