-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
368 lines (300 loc) · 11.1 KB
/
MainWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
using Microsoft.Win32;
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
using System.Windows.Media.Media3D;
namespace AtomMediaPlayer
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private bool userIsDraggingSlider = false;
private bool repeatIndefinitely = false;
private bool fullscreen = false;
private bool continueLoop = false;
TimeSpan startLoop, endLoop;
private DispatcherTimer DoubleClickTimer = new DispatcherTimer();
private DispatcherTimer loopTimer;
public MainWindow()
{
InitializeComponent();
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += timer_Tick;
timer.Start();
DoubleClickTimer.Interval = TimeSpan.FromMilliseconds(GetDoubleClickTime());
DoubleClickTimer.Tick += (s, e) => DoubleClickTimer.Stop();
loopTimer = new DispatcherTimer();
loopTimer.Interval = TimeSpan.FromSeconds(1);
loopTimer.Tick += new EventHandler(loopTimer_Tick);
}
private void loopTimer_Tick(object sender, EventArgs e)
{
//At the end of a Tick period, reset the MediaElement Position and Play again
if (AtomPlayer.Source != null && AtomPlayer.Position.TotalSeconds > endLoop.TotalSeconds && continueLoop)
{
if (continueLoop)
{
AtomPlayer.Position = startLoop;
}
//Restart the timer
loopTimer.Start();
}
}
private void timer_Tick(object sender, EventArgs e)
{
if ((AtomPlayer.Source != null) && (AtomPlayer.NaturalDuration.HasTimeSpan) && (!userIsDraggingSlider))
{
sliProgress.Minimum = 0;
sliProgress.Maximum = AtomPlayer.NaturalDuration.TimeSpan.TotalSeconds;
sliProgress.Value = AtomPlayer.Position.TotalSeconds;
}
}
private void BrowseButtonClick(object sender, RoutedEventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "Media Files (*.wmv;*.mp4;*.mp3;*.wav;*.avi;*.mkv)|*.wmv;*.mp4;*.mp3;*.wav;*.avi;*.mkv|All Files (*.*)|*.*";
dlg.RestoreDirectory = true;
dlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
if (dlg.ShowDialog() == true)
{
string selectedFileName = dlg.FileName;
AtomPlayer.Source = new Uri(selectedFileName);
AtomPlayer.Play();
loopTimer.Start();
this.Title = Path.GetFileNameWithoutExtension(selectedFileName);
}
}
private void btnPlay_Click(object sender, RoutedEventArgs e)
{
AtomPlayer.Play();
}
private void btnPause_Click(object sender, RoutedEventArgs e)
{
AtomPlayer.Pause();
}
private void btnStop_Click(object sender, RoutedEventArgs e)
{
AtomPlayer.Stop();
loopTimer.Stop();
}
private void sliProgress_DragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e)
{
userIsDraggingSlider = true;
}
private void sliProgress_DragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
{
userIsDraggingSlider = false;
AtomPlayer.Position = TimeSpan.FromSeconds(sliProgress.Value);
}
private void sliProgress_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
lblProgressStatus.Text = TimeSpan.FromSeconds(sliProgress.Value).ToString(@"hh\:mm\:ss");
}
private void Grid_MouseWheel(object sender, MouseWheelEventArgs e)
{
AtomPlayer.Volume += (e.Delta > 0) ? 0.1 : -0.1;
}
private void AtomPlayer_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (!DoubleClickTimer.IsEnabled)
{
DoubleClickTimer.Start();
}
else
{
if (!fullscreen)
{
this.WindowStyle = WindowStyle.None;
this.WindowState = WindowState.Maximized;
Grid.SetRowSpan(AtomPlayer, 3);
AtomPlayer.Margin = new Thickness(0, 0, 0, 0);
AtomPlayer.Stretch = Stretch.Fill;
Panel.SetZIndex(statusBar, 3);
Panel.SetZIndex(btnPlay, 3);
Panel.SetZIndex(btnStop, 3);
Panel.SetZIndex(btnPause, 3);
Panel.SetZIndex(gridButton, 3);
Panel.SetZIndex(gridStatusBar, 3);
statusBar.Opacity = 0;
btnPlay.Opacity = 0;
btnPause.Opacity = 0;
btnStop.Opacity = 0;
}
else
{
this.WindowStyle = WindowStyle.SingleBorderWindow;
this.WindowState = WindowState.Normal;
Grid.SetRowSpan(AtomPlayer, 2);
AtomPlayer.Margin = new Thickness(0, 20, 0, 63);
AtomPlayer.Stretch = Stretch.Uniform;
Panel.SetZIndex(statusBar, 1);
Panel.SetZIndex(btnPlay, 1);
Panel.SetZIndex(btnStop, 1);
Panel.SetZIndex(btnPause, 1);
Panel.SetZIndex(gridButton, 1);
Panel.SetZIndex(gridStatusBar, 1);
statusBar.Opacity = 1;
btnPlay.Opacity = 1;
btnPause.Opacity = 1;
btnStop.Opacity = 1;
statusBar.IsEnabled = true;
}
fullscreen = !fullscreen;
}
}
private void ChangeMediaSpeedRatio(object sender, RoutedPropertyChangedEventArgs<double> args)
{
AtomPlayer.SpeedRatio = (double)speedRatioSlider.Value;
}
private void mediaElement_MediaFailed(object sender, ExceptionRoutedEventArgs e)
{
MessageBox.Show(e.ErrorException.Message);
}
[DllImport("user32.dll")]
private static extern uint GetDoubleClickTime();
private void StatusBar_MouseEnter(object sender, MouseEventArgs e)
{
if (fullscreen)
{
statusBar.Opacity = 1;
statusBar.IsEnabled = true;
}
}
private void statusBar_MouseLeave(object sender, MouseEventArgs e)
{
if (fullscreen)
{
statusBar.Opacity = 0;
statusBar.IsEnabled = false;
}
}
private void btn_MouseEnter(object sender, MouseEventArgs e)
{
if (fullscreen)
{
btnPlay.Opacity = 1;
btnPause.Opacity = 1;
btnStop.Opacity = 1;
btnPause.IsEnabled = true;
btnPlay.IsEnabled = true;
btnStop.IsEnabled = true;
}
}
private void btn_MouseLeave(object sender, MouseEventArgs e)
{
if (fullscreen)
{
btnPlay.Opacity = 0;
btnPause.Opacity = 0;
btnStop.Opacity = 0;
btnPause.IsEnabled = false;
btnPlay.IsEnabled = false;
btnStop.IsEnabled = false;
}
}
private void btnAbout_Click(object sender, RoutedEventArgs e)
{
About a = new About();
if (a.ShowDialog() == true)
{
}
else
{
return;
}
}
private void btnBrowse_MouseEnter(object sender, MouseEventArgs e)
{
btnBrowse.TextDecorations = TextDecorations.Underline;
}
private void btnBrowse_MouseLeave(object sender, MouseEventArgs e)
{
btnBrowse.TextDecorations = null;
}
private void btnExit_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
this.Close();
}
private void btnExit_MouseEnter(object sender, MouseEventArgs e)
{
btnExit.TextDecorations = TextDecorations.Underline;
}
private void btnExit_MouseLeave(object sender, MouseEventArgs e)
{
btnExit.TextDecorations = null;
}
private void btnHelp_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
Help h = new Help();
if (h.ShowDialog() == true)
{
}
else
{
return;
}
}
private void btnHelp_MouseEnter(object sender, MouseEventArgs e)
{
btnHelp.TextDecorations = TextDecorations.Underline;
}
private void btnHelp_MouseLeave(object sender, MouseEventArgs e)
{
btnHelp.TextDecorations = null;
}
private void btnAbout_MouseEnter(object sender, MouseEventArgs e)
{
btnAbout.TextDecorations = TextDecorations.Underline;
}
private void btnAbout_MouseLeave(object sender, MouseEventArgs e)
{
btnAbout.TextDecorations = null;
}
private void btnLoop_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
Loop a = new Loop();
if(a.ShowDialog() == true){
continueLoop = !continueLoop;
startLoop = a.from;
endLoop = a.to;
MessageBox.Show(startLoop.ToString());
MessageBox.Show(endLoop.ToString());
}
else
{
continueLoop = false;
startLoop = TimeSpan.FromSeconds(0);
endLoop = TimeSpan.FromSeconds(0);
return;
}
}
private void btnRepeat_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
repeatIndefinitely = !repeatIndefinitely;
if (repeatIndefinitely == true)
{
repeat.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF75949B"));
}
else
{
repeat.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#0087A2"));
}
}
private void AtomPlayer_MediaEnded(object sender, RoutedEventArgs e)
{
if (repeatIndefinitely)
{
AtomPlayer.Position = TimeSpan.FromSeconds(0);
AtomPlayer.Play();
}
}
}
}