Skip to content

Commit c45056b

Browse files
committed
Merge branch 'audioplayer-status'
- Merging progress bar fix branch into main.
2 parents 2426acd + 3401f9b commit c45056b

File tree

5 files changed

+79
-54
lines changed

5 files changed

+79
-54
lines changed

src/.editorconfig

+3
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@
22

33
# SA1118: Parameter should not span multiple lines
44
dotnet_diagnostic.SA1118.severity = none
5+
6+
# IDE0003: Remove qualification
7+
dotnet_diagnostic.IDE0003.severity = none

src/Enums/PlayerStatus.cs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace MusicSharp
2+
{
3+
/// <summary>
4+
/// The status of the audio player.
5+
/// </summary>
6+
public enum PlayerStatus
7+
{
8+
Playing,
9+
Paused,
10+
Stopped
11+
}
12+
}

src/model/IPlayer.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public interface IPlayer
1212
/// <summary>
1313
/// Gets or sets a value indicating whether the audio player is playing.
1414
/// </summary>
15-
bool IsAudioPlaying { get; set; }
15+
PlayerStatus PlayerStatus { get; set; }
1616

1717
/// <summary>
1818
/// Gets or sets the last file opened by the player.

src/model/WinPlayer.cs

+15-12
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public WinPlayer()
2626
}
2727

2828
/// <inheritdoc/>
29-
public bool IsAudioPlaying { get; set; } = false;
29+
public PlayerStatus PlayerStatus { get; set; } = PlayerStatus.Stopped;
3030

3131
/// <inheritdoc/>
3232
public string LastFileOpened { get; set; }
@@ -35,7 +35,7 @@ public WinPlayer()
3535
public void Stop()
3636
{
3737
this.outputDevice.Stop();
38-
this.IsAudioPlaying = false;
38+
this.PlayerStatus = PlayerStatus.Stopped;
3939
}
4040

4141
/// <summary>
@@ -50,7 +50,7 @@ public void OpenFile(string path)
5050
this.audioFileReader = new AudioFileReader(path);
5151
this.outputDevice.Init(this.audioFileReader);
5252
this.outputDevice.Play();
53-
this.IsAudioPlaying = true;
53+
this.PlayerStatus = PlayerStatus.Playing;
5454
}
5555
else
5656
{
@@ -63,19 +63,22 @@ public void OpenFile(string path)
6363
/// </summary>
6464
public void PlayPause()
6565
{
66-
if (
67-
this.outputDevice.PlaybackState == PlaybackState.Paused ||
68-
this.outputDevice.PlaybackState == PlaybackState.Stopped)
66+
if (this.outputDevice.PlaybackState == PlaybackState.Stopped)
6967
{
7068
this.outputDevice.Play();
71-
this.IsAudioPlaying = true;
69+
this.PlayerStatus = PlayerStatus.Playing;
7270
return;
7371
}
74-
75-
if (this.outputDevice.PlaybackState == PlaybackState.Playing)
72+
else if (this.outputDevice.PlaybackState == PlaybackState.Paused)
73+
{
74+
this.outputDevice.Play();
75+
this.PlayerStatus = PlayerStatus.Playing;
76+
return;
77+
}
78+
else if (this.outputDevice.PlaybackState == PlaybackState.Playing)
7679
{
7780
this.outputDevice.Pause();
78-
this.IsAudioPlaying = false;
81+
this.PlayerStatus = PlayerStatus.Paused;
7982
}
8083
}
8184

@@ -91,7 +94,7 @@ public void PlayFromPlaylist(string path)
9194
this.audioFileReader = new AudioFileReader(path);
9295
this.outputDevice.Init(this.audioFileReader);
9396
this.outputDevice.Play();
94-
this.IsAudioPlaying = true;
97+
this.PlayerStatus = PlayerStatus.Playing;
9598
}
9699
catch (System.IO.FileNotFoundException)
97100
{
@@ -173,7 +176,7 @@ public System.TimeSpan CurrentTime()
173176
{
174177
TimeSpan zeroTime = new TimeSpan(0);
175178

176-
if (this.outputDevice.PlaybackState != PlaybackState.Stopped && this.outputDevice.PlaybackState != PlaybackState.Paused)
179+
if (this.outputDevice.PlaybackState != PlaybackState.Stopped)
177180
{
178181
return this.audioFileReader.CurrentTime;
179182
}

src/view/Tui.cs

+48-41
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,13 @@ public class Tui
2424
private static StatusBar statusBar;
2525

2626
private static Label trackName;
27-
private static Label trackLength;
2827

2928
/// <summary>
3029
/// Create a new instance of the audio player engine.
3130
/// </summary>
3231
private readonly IPlayer player;
3332

3433
private object mainLoopTimeout = null;
35-
private uint mainLooopTimeoutTick = 1000; // ms
3634

3735
private List<string> playlist = new List<string>();
3836
private PlaylistLoader playlistLoader = new PlaylistLoader();
@@ -108,12 +106,19 @@ public void Start()
108106
playPauseButton.Clicked += () =>
109107
{
110108
this.PlayPause();
109+
110+
if (this.player.PlayerStatus != PlayerStatus.Stopped)
111+
{
112+
this.UpdateProgressBar();
113+
}
111114
};
112115

113116
var stopButton = new Button(16, 1, "Stop");
114117
stopButton.Clicked += () =>
115118
{
116119
this.player.Stop();
120+
this.AudioProgressBar.Fraction = 0F;
121+
this.TimePlayedLabel();
117122
};
118123

119124
var seekForward = new Button(26, 0, "Seek 5s");
@@ -171,8 +176,7 @@ public void Start()
171176
this.player.LastFileOpened = a.Value.ToString();
172177
this.player.PlayFromPlaylist(this.player.LastFileOpened);
173178
this.NowPlaying(this.player.LastFileOpened);
174-
this.TrackLength();
175-
this.TimePlayed();
179+
this.UpdateProgressBar();
176180
};
177181

178182
playlistPane.Add(playlistView);
@@ -209,7 +213,11 @@ private void PlayPause()
209213
try
210214
{
211215
this.player.PlayPause();
212-
this.TimePlayed();
216+
217+
if (this.player.PlayerStatus == PlayerStatus.Playing)
218+
{
219+
this.UpdateProgressBar();
220+
}
213221
}
214222
catch (Exception)
215223
{
@@ -232,11 +240,12 @@ private void OpenFile()
232240
{
233241
if (File.Exists(d.FilePath.ToString()))
234242
{
235-
this.player.LastFileOpened = d.FilePath.ToString();
236-
this.player.OpenFile(this.player.LastFileOpened);
237-
this.NowPlaying(this.player.LastFileOpened);
238-
this.TrackLength();
239-
this.TimePlayed();
243+
this.player.LastFileOpened = d.FilePath.ToString();
244+
this.player.OpenFile(this.player.LastFileOpened);
245+
this.NowPlaying(this.player.LastFileOpened);
246+
this.AudioProgressBar.Fraction = 0F;
247+
this.UpdateProgressBar();
248+
this.TimePlayedLabel();
240249
}
241250
else
242251
{
@@ -324,46 +333,44 @@ private void NowPlaying(string track)
324333
nowPlaying.Add(trackName);
325334
}
326335

327-
private void TrackLength()
336+
private void TimePlayedLabel()
328337
{
329-
trackLength = new Label(this.player.TrackLength().ToString(@"mm\:ss"))
338+
if (this.player.PlayerStatus != PlayerStatus.Stopped)
330339
{
331-
X = Pos.Right(this.AudioProgressBar) + 7,
332-
Y = 2,
333-
};
334-
335-
nowPlaying.Add(trackLength);
336-
}
337-
338-
private void TimePlayed()
339-
{
340-
this.AudioProgressBar.Fraction = 0F;
341-
342-
double counter = Convert.ToInt32(this.player.TrackLength().TotalSeconds);
343-
344-
this.mainLoopTimeout = Application.MainLoop.AddTimeout(TimeSpan.FromMilliseconds(this.mainLooopTimeoutTick), (loop) =>
340+
var timePlayed = this.player.CurrentTime().ToString(@"mm\:ss");
341+
var trackLength = this.player.TrackLength().ToString(@"mm\:ss");
342+
trackName = new Label($"{timePlayed} / {trackLength}")
345343
{
346-
while (counter != 0 && this.player.IsAudioPlaying)
347-
{
348-
this.AudioProgressBar.Fraction += (float)(1 / this.player.TrackLength().TotalSeconds);
349-
this.TimePlayedLabel(this.player.CurrentTime().ToString(@"mm\:ss"));
350-
counter--;
351-
return true;
352-
}
344+
X = Pos.Right(this.AudioProgressBar),
345+
Y = 2,
346+
};
347+
}
348+
else
349+
{
350+
trackName = new Label($"00:00 / 00:00")
351+
{
352+
X = Pos.Right(this.AudioProgressBar),
353+
Y = 2,
354+
};
355+
}
353356

354-
return false;
355-
});
357+
nowPlaying.Add(trackName);
356358
}
357359

358-
private void TimePlayedLabel(string timePlayed)
360+
private void UpdateProgressBar()
359361
{
360-
trackName = new Label($"{timePlayed} / ")
362+
this.mainLoopTimeout = Application.MainLoop.AddTimeout(TimeSpan.FromSeconds(1), (updateTimer) =>
361363
{
362-
X = Pos.Right(this.AudioProgressBar),
363-
Y = 2,
364-
};
364+
while (this.player.CurrentTime().Seconds < this.player.TrackLength().TotalSeconds && this.player.PlayerStatus is not PlayerStatus.Stopped)
365+
{
366+
this.AudioProgressBar.Fraction = (float)(this.player.CurrentTime().Seconds / this.player.TrackLength().TotalSeconds);
367+
this.TimePlayedLabel();
365368

366-
nowPlaying.Add(trackName);
369+
return true;
370+
}
371+
372+
return false;
373+
});
367374
}
368375
}
369376
}

0 commit comments

Comments
 (0)