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
17 changes: 17 additions & 0 deletions src/model/IPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ namespace MusicSharp
/// </summary>
public interface IPlayer
{
/// <summary>
///Gets or sets if the audio player is playing.
/// </summary>
bool IsAudioPlaying { get; set; }

/// <summary>
/// Gets or sets the last file opened by the player.
/// </summary>
Expand Down Expand Up @@ -51,5 +56,17 @@ public interface IPlayer
/// </summary>
/// <param name="path">The path to the audio file.</param>
void PlayFromPlaylist(string path);

/// <summary>
/// Returns the current playtime of the audioFileReader instance.
/// </summary>
/// <returns>The current time played as TimeSpan.</returns>
System.TimeSpan CurrentTime();

/// <summary>
/// Returns the total track length in timespan format.
/// </summary>
/// <returns>The length of the track in timespan format.</returns>
System.TimeSpan TrackLength();
}
}
32 changes: 31 additions & 1 deletion src/model/WinPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace MusicSharp
{
using System;
using System.IO;
using NAudio.Wave;

Expand All @@ -24,13 +25,16 @@ public WinPlayer()
this.outputDevice.PlaybackStopped += this.OnPlaybackStopped;
}

public bool IsAudioPlaying { get; set; } = false;

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

/// <inheritdoc/>
public void Stop()
{
this.outputDevice.Stop();
this.IsAudioPlaying = false;
}

/// <summary>
Expand All @@ -40,11 +44,12 @@ public void Stop()
public void OpenFile(string path)
{
bool isFileValid = File.Exists(path);
if (isFileValid == true)
if (isFileValid)
{
this.audioFileReader = new AudioFileReader(path);
this.outputDevice.Init(this.audioFileReader);
this.outputDevice.Play();
this.IsAudioPlaying = true;
}
else
{
Expand All @@ -62,12 +67,14 @@ public void PlayPause()
this.outputDevice.PlaybackState == PlaybackState.Stopped)
{
this.outputDevice.Play();
this.IsAudioPlaying = true;
return;
}

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

Expand All @@ -83,6 +90,7 @@ public void PlayFromPlaylist(string path)
this.audioFileReader = new AudioFileReader(path);
this.outputDevice.Init(this.audioFileReader);
this.outputDevice.Play();
this.IsAudioPlaying = true;
}
catch (System.IO.FileNotFoundException)
{
Expand Down Expand Up @@ -149,6 +157,7 @@ public void OpenStream(string streamURL)
{
this.outputDevice.Init(mf);
this.outputDevice.Play();

}
}
catch (System.ArgumentException)
Expand All @@ -158,5 +167,26 @@ public void OpenStream(string streamURL)
{
}
}

/// <inheritdoc/>
public System.TimeSpan CurrentTime()
{
TimeSpan zeroTime = new TimeSpan(0);

if (this.outputDevice.PlaybackState != PlaybackState.Stopped && this.outputDevice.PlaybackState != PlaybackState.Paused)
{
return this.audioFileReader.CurrentTime;
}
else
{
return zeroTime;
}
}

/// <inheritdoc/>
public System.TimeSpan TrackLength()
{
return this.audioFileReader.TotalTime;
}
}
}
81 changes: 72 additions & 9 deletions src/view/Tui.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace MusicSharp
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading;
using Terminal.Gui;

/// <summary>
Expand All @@ -23,12 +24,16 @@ public class Tui
private static StatusBar statusBar;

private static Label trackName;
private static Label trackLength;

/// <summary>
/// Create a new instance of the audio player engine.
/// </summary>
private readonly IPlayer player;

private object mainLoopTimeout = null;
private uint mainLooopTimeoutTick = 1000; // ms

private List<string> playlist = new List<string>();
private PlaylistLoader playlistLoader = new PlaylistLoader();

Expand Down Expand Up @@ -94,7 +99,7 @@ public void Start()
{
X = 0,
Y = 24,
Width = 70,
Width = 55,
Height = 5,
CanFocus = true,
};
Expand All @@ -111,19 +116,31 @@ public void Start()
this.player.Stop();
};

var increaseVolumeButton = new Button(55, 0, "+ Volume");
var seekForward = new Button(26, 0, "Seek 5s");
stopButton.Clicked += () =>
{
this.player.Stop();
};

var seekBackward = new Button(26, 2, "Seek -5s");
stopButton.Clicked += () =>
{
this.player.Stop();
};

var increaseVolumeButton = new Button(39, 0, "+ Volume");
increaseVolumeButton.Clicked += () =>
{
this.player.IncreaseVolume();
};

var decreaseVolumeButton = new Button(55, 2, "- Volume");
var decreaseVolumeButton = new Button(39, 2, "- Volume");
decreaseVolumeButton.Clicked += () =>
{
this.player.DecreaseVolume();
};

playbackControls.Add(playPauseButton, stopButton, increaseVolumeButton, decreaseVolumeButton);
playbackControls.Add(playPauseButton, stopButton, increaseVolumeButton, decreaseVolumeButton, seekBackward, seekForward);

// Create the left-hand playlists view.
playlistPane = new FrameView("Playlist Tracks")
Expand Down Expand Up @@ -154,14 +171,16 @@ public void Start()
this.player.LastFileOpened = a.Value.ToString();
this.player.PlayFromPlaylist(this.player.LastFileOpened);
this.NowPlaying(this.player.LastFileOpened);
this.TrackLength();
this.TimePlayed();
};

playlistPane.Add(playlistView);

// Create the audio progress bar frame.
nowPlaying = new FrameView("Now Playing")
{
X = 70,
X = 55,
Y = 24,
Width = Dim.Fill(),
Height = 5,
Expand All @@ -170,12 +189,11 @@ public void Start()

this.AudioProgressBar = new ProgressBar()
{
X = 1,
X = 0,
Y = 2,
Width = Dim.Fill() - 1,
Width = Dim.Fill() - 15,
Height = 1,
Fraction = 0.4F,
ColorScheme = Colors.Error,
ColorScheme = Colors.Base,
};

nowPlaying.Add(this.AudioProgressBar);
Expand All @@ -191,6 +209,7 @@ private void PlayPause()
try
{
this.player.PlayPause();
this.TimePlayed();
}
catch (Exception)
{
Expand All @@ -216,6 +235,8 @@ private void OpenFile()
this.player.LastFileOpened = d.FilePath.ToString();
this.player.OpenFile(this.player.LastFileOpened);
this.NowPlaying(this.player.LastFileOpened);
this.TrackLength();
this.TimePlayed();
}
else
{
Expand Down Expand Up @@ -302,5 +323,47 @@ private void NowPlaying(string track)

nowPlaying.Add(trackName);
}

private void TrackLength()
{
trackLength = new Label(this.player.TrackLength().ToString(@"mm\:ss"))
{
X = Pos.Right(this.AudioProgressBar) + 7,
Y = 2,
};

nowPlaying.Add(trackLength);
}

private void TimePlayed()
{
this.AudioProgressBar.Fraction = 0F;

double counter = Convert.ToInt32(this.player.TrackLength().TotalSeconds);

this.mainLoopTimeout = Application.MainLoop.AddTimeout(TimeSpan.FromMilliseconds(this.mainLooopTimeoutTick), (loop) =>
{
while (counter != 0 && this.player.IsAudioPlaying)
{
this.AudioProgressBar.Fraction += (float)(1 / this.player.TrackLength().TotalSeconds);
this.TimePlayedLabel(this.player.CurrentTime().ToString(@"mm\:ss"));
counter--;
return true;
}

return false;
});
}

private void TimePlayedLabel(string timePlayed)
{
trackName = new Label($"{timePlayed} / ")
{
X = Pos.Right(this.AudioProgressBar),
Y = 2,
};

nowPlaying.Add(trackName);
}
}
}