Skip to content

Commit c93fc8c

Browse files
committed
Fixed #2
Progress Slider now updates Media Playback in a Smooth Manner
1 parent 01f348f commit c93fc8c

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

CSediaPlayer/MainWindow.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<Button x:Name="pause" Content="Pause" HorizontalAlignment="Left" Height="51" Margin="523,0,0,315" VerticalAlignment="Bottom" Width="146" Click="Pause"/>
1313
<Button x:Name="stop" Content="Stop" HorizontalAlignment="Right" Height="51" Margin="0,0,574,315" VerticalAlignment="Bottom" Width="136" Click="Stop"/>
1414
<Button x:Name="select" Content="Song Select" HorizontalAlignment="Right" Height="51" Margin="0,0,422,315" VerticalAlignment="Bottom" Width="147" Click="Select"/>
15-
<Slider x:Name="Progress_Slider" Height="30" Margin="0,275,123,0" VerticalAlignment="Top" Value="0.1" HorizontalAlignment="Right" Width="587" Thumb.DragStarted="Progress_Slider_DragStarted" Thumb.DragCompleted="Progress_Slider_DragCompleted"/>
15+
<Slider x:Name="Progress_Slider" Height="30" Margin="0,275,123,0" VerticalAlignment="Top" Value="0.1" HorizontalAlignment="Right" Width="587" Thumb.DragStarted="Progress_Slider_DragStarted" Thumb.DragCompleted="Progress_Slider_DragCompleted" Thumb.DragDelta="Progress_Slider_DragDelta"/>
1616
<TextBlock x:Name="Current_Playing" HorizontalAlignment="Right" Height="56" Margin="0,173,10,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="601"><Run Text="Now Playing"/><LineBreak/><Run/></TextBlock>
1717
<TextBlock x:Name="Current_Time" HorizontalAlignment="Left" Margin="55,275,0,0" TextWrapping="Wrap" Text="0:00" VerticalAlignment="Top"/>
1818
<TextBlock x:Name="Max_Time" HorizontalAlignment="Right" Margin="0,275,96,0" TextWrapping="Wrap" Text="0:00" VerticalAlignment="Top"/>

CSediaPlayer/MainWindow.xaml.cs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Diagnostics;
23
using System.Threading;
34
using System.Windows;
45
using System.Windows.Threading;
@@ -17,7 +18,9 @@ public partial class MainWindow : Window
1718
double volume;
1819
double prevVolume;
1920
bool ProgressSliderChanging = false;
20-
string path;
21+
double LastProgressSliderThumbDeltaValue = 0;
22+
Stopwatch LastProgressSliderDeltaEvent = new Stopwatch();
23+
string path;
2124
Thread LoopingThread;
2225

2326

@@ -149,11 +152,36 @@ private void Close(object sender, System.ComponentModel.CancelEventArgs e)
149152
private void Progress_Slider_DragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e)
150153
{
151154
ProgressSliderChanging = true;
155+
double NewPosition = Progress_Slider.Value;
156+
player.controls.currentPosition = NewPosition;
157+
LastProgressSliderThumbDeltaValue = NewPosition;
158+
LastProgressSliderDeltaEvent.Start();
152159
}
153160

154161
private void Progress_Slider_DragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
155162
{
156163
ProgressSliderChanging = false;
164+
double NewPosition = Progress_Slider.Value;
165+
player.controls.currentPosition = NewPosition;
166+
LastProgressSliderThumbDeltaValue = NewPosition;
167+
LastProgressSliderDeltaEvent.Stop();
168+
}
169+
170+
private void Progress_Slider_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
171+
{
172+
//DragDelta runs very often - to prevent lag we throw most of these events out
173+
if (LastProgressSliderDeltaEvent.ElapsedMilliseconds<300)
174+
{
175+
return;
176+
}
177+
178+
double NewPosition = Progress_Slider.Value;
179+
if (Math.Abs(LastProgressSliderThumbDeltaValue-NewPosition) > 1)
180+
{
181+
player.controls.currentPosition = NewPosition;
182+
LastProgressSliderThumbDeltaValue = NewPosition;
183+
LastProgressSliderDeltaEvent.Restart();
184+
}
157185
}
158186
}
159187
}

0 commit comments

Comments
 (0)