Skip to content

Commit 8c24c2e

Browse files
committed
Fixed seek bar showing song progress, closes #1.
1 parent 02e72a6 commit 8c24c2e

File tree

2 files changed

+33
-28
lines changed

2 files changed

+33
-28
lines changed

CSediaPlayer/MainWindow.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
66
xmlns:local="clr-namespace:WpfApp1"
77
mc:Ignorable="d"
8-
Title="MainWindow" Height="450" Width="800">
8+
Title="MainWindow" Height="450" Width="800" Closing="Close">
99
<Grid>
1010
<TextBlock x:Name="Current_Playing_Text" HorizontalAlignment="Left" Height="22.029" Margin="82.331,173.403,0,0" TextWrapping="Wrap" Text="Currently Playing:" VerticalAlignment="Top" Width="93.324"/>
1111
<Button x:Name="play" Content="Play" HorizontalAlignment="Left" Height="50.719" Margin="375.173,53.403,0,0" VerticalAlignment="Top" Width="142.446" Click="Play"/>

CSediaPlayer/MainWindow.xaml.cs

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public partial class MainWindow : Window
1717
double volume;
1818
double prevVolume;
1919
string path;
20+
bool FirstRun =true;
21+
Thread LoopingThread;
2022

2123

2224
WMPLib.WindowsMediaPlayer player;
@@ -33,8 +35,10 @@ public MainWindow()
3335
player.uiMode = "full";
3436
VolumeUpdate();
3537
SongSelect();
38+
LoopingThread = new Thread(Looper);
39+
LoopingThread.Start();
40+
3641

37-
player.PositionChange += UpdateSongPosition;
3842
//DelayAction(3000, TimerLoop);
3943

4044

@@ -55,45 +59,41 @@ private void SongSelect()
5559
{
5660
path = file.FileName;
5761
}
58-
else
59-
{
60-
SongSelect();
61-
}
6262
Console.WriteLine(path);
6363
player.URL = path;
6464
Current_Playing.Text = player.currentMedia.name;
65-
Progress_Slider.Maximum = Convert.ToInt32(player.currentMedia.duration);
66-
Console.WriteLine(player.currentMedia.durationString);
67-
Max_Time.Text = player.currentMedia.durationString;
68-
69-
70-
7165

7266

7367
}
7468

75-
private void TimerLoop()
69+
70+
private void Looper()
7671
{
77-
try
72+
if (FirstRun)
7873
{
79-
while (Progress_Slider.Value != player.controls.currentPosition * 100 / player.currentMedia.duration)
80-
{
81-
82-
Progress_Slider.Value = player.controls.currentPosition * 100 / player.currentMedia.duration;
83-
84-
85-
}
74+
Thread.Sleep(50);
75+
FirstRun = false;
8676
}
87-
catch
77+
while (true)
8878
{
89-
79+
System.Threading.Thread.Sleep(100);
80+
81+
82+
if (player.currentMedia.duration > 1 && player.controls.currentPosition > 1)
83+
{
84+
this.Dispatcher.Invoke(() =>
85+
{
86+
UpdateSongPosition();
87+
});
88+
}
89+
90+
9091
}
9192
}
92-
93-
private void UpdateSongPosition(double a, double b)
94-
{
9593

96-
Progress_Slider.Value = player.controls.currentPosition * 100 / player.currentMedia.duration;
94+
private void UpdateSongPosition()
95+
{
96+
Progress_Slider.Value = player.controls.currentPosition;
9797
Current_Time.Text = player.controls.currentPositionString;
9898
Progress_Slider.Maximum = player.currentMedia.duration;
9999
Max_Time.Text = player.currentMedia.durationString;
@@ -165,7 +165,12 @@ public static void DelayAction(int millisecond, Action action)
165165

166166
private void temp(object sender, RoutedEventArgs e)
167167
{
168-
UpdateSongPosition(0, 0);
168+
UpdateSongPosition();
169+
}
170+
171+
private void Close(object sender, System.ComponentModel.CancelEventArgs e)
172+
{
173+
LoopingThread.Abort();
169174
}
170175
}
171176
}

0 commit comments

Comments
 (0)