@@ -24,15 +24,13 @@ public class Tui
24
24
private static StatusBar statusBar ;
25
25
26
26
private static Label trackName ;
27
- private static Label trackLength ;
28
27
29
28
/// <summary>
30
29
/// Create a new instance of the audio player engine.
31
30
/// </summary>
32
31
private readonly IPlayer player ;
33
32
34
33
private object mainLoopTimeout = null ;
35
- private uint mainLooopTimeoutTick = 1000 ; // ms
36
34
37
35
private List < string > playlist = new List < string > ( ) ;
38
36
private PlaylistLoader playlistLoader = new PlaylistLoader ( ) ;
@@ -108,12 +106,19 @@ public void Start()
108
106
playPauseButton . Clicked += ( ) =>
109
107
{
110
108
this . PlayPause ( ) ;
109
+
110
+ if ( this . player . PlayerStatus != PlayerStatus . Stopped )
111
+ {
112
+ this . UpdateProgressBar ( ) ;
113
+ }
111
114
} ;
112
115
113
116
var stopButton = new Button ( 16 , 1 , "Stop" ) ;
114
117
stopButton . Clicked += ( ) =>
115
118
{
116
119
this . player . Stop ( ) ;
120
+ this . AudioProgressBar . Fraction = 0F ;
121
+ this . TimePlayedLabel ( ) ;
117
122
} ;
118
123
119
124
var seekForward = new Button ( 26 , 0 , "Seek 5s" ) ;
@@ -171,8 +176,7 @@ public void Start()
171
176
this . player . LastFileOpened = a . Value . ToString ( ) ;
172
177
this . player . PlayFromPlaylist ( this . player . LastFileOpened ) ;
173
178
this . NowPlaying ( this . player . LastFileOpened ) ;
174
- this . TrackLength ( ) ;
175
- this . TimePlayed ( ) ;
179
+ this . UpdateProgressBar ( ) ;
176
180
} ;
177
181
178
182
playlistPane . Add ( playlistView ) ;
@@ -209,7 +213,11 @@ private void PlayPause()
209
213
try
210
214
{
211
215
this . player . PlayPause ( ) ;
212
- this . TimePlayed ( ) ;
216
+
217
+ if ( this . player . PlayerStatus == PlayerStatus . Playing )
218
+ {
219
+ this . UpdateProgressBar ( ) ;
220
+ }
213
221
}
214
222
catch ( Exception )
215
223
{
@@ -232,11 +240,12 @@ private void OpenFile()
232
240
{
233
241
if ( File . Exists ( d . FilePath . ToString ( ) ) )
234
242
{
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 ( ) ;
240
249
}
241
250
else
242
251
{
@@ -324,46 +333,44 @@ private void NowPlaying(string track)
324
333
nowPlaying . Add ( trackName ) ;
325
334
}
326
335
327
- private void TrackLength ( )
336
+ private void TimePlayedLabel ( )
328
337
{
329
- trackLength = new Label ( this . player . TrackLength ( ) . ToString ( @"mm\:ss" ) )
338
+ if ( this . player . PlayerStatus != PlayerStatus . Stopped )
330
339
{
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 } ")
345
343
{
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
+ }
353
356
354
- return false ;
355
- } ) ;
357
+ nowPlaying . Add ( trackName ) ;
356
358
}
357
359
358
- private void TimePlayedLabel ( string timePlayed )
360
+ private void UpdateProgressBar ( )
359
361
{
360
- trackName = new Label ( $ " { timePlayed } / " )
362
+ this . mainLoopTimeout = Application . MainLoop . AddTimeout ( TimeSpan . FromSeconds ( 1 ) , ( updateTimer ) =>
361
363
{
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 ( ) ;
365
368
366
- nowPlaying . Add ( trackName ) ;
369
+ return true ;
370
+ }
371
+
372
+ return false ;
373
+ } ) ;
367
374
}
368
375
}
369
376
}
0 commit comments