-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
368 lines (355 loc) · 16.5 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 System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using Microsoft.Win32;
using WMPLib;
namespace Course_Project_WPF
{
public partial class MainWindow : Window
{
WindowsMediaPlayer Player = new WindowsMediaPlayer();
bool isRepeat = false;
int CurrentMediaIndex;
public MainWindow()
{
InitializeComponent();
Player.settings.autoStart = false;
Player.PlayStateChange += Player_PlayStateChange;
}
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
if(Player.playState == WMPPlayState.wmppsReady)
{
PlayerToggle();
(sender as System.Windows.Threading.DispatcherTimer).Stop();
}
}
private void Player_PlayStateChange(int NewState)
{
// MessageBox.Show(NewState.ToString());
if(NewState == 3) { SongNameLabel.Content = String.Join(", ",Playlist[CurrentMediaIndex].Performers) + " - " + Playlist[CurrentMediaIndex].Title; }
if (NewState == 8)
{
if (!Player.settings.getMode("loop"))
{
PathGeometry pg = new PathGeometry();
pg.FillRule = FillRule.Nonzero;
PathFigureCollectionConverter pfcc = new PathFigureCollectionConverter();
pg.Figures = (PathFigureCollection)pfcc.ConvertFrom("M8 5v14l11-7z");
ToggleButtonPath.Data = pg;
((Path)FindName("PlayTrackButton" + CurrentMediaIndex.ToString() + "Path")).Data = pg;
Player.controls.stop();
if (isRepeat)
{
if (CurrentMediaIndex + 1 == Playlist.Count)
{
CurrentMediaIndex = 0;
Player.currentMedia = Playlist[CurrentMediaIndex].Media;
}
else
{
CurrentMediaIndex++;
Player.currentMedia = Playlist[CurrentMediaIndex].Media;
}
}
else
{
if (CurrentMediaIndex + 1 != Playlist.Count)
{
CurrentMediaIndex++;
Player.currentMedia = Playlist[CurrentMediaIndex].Media;
}
}
System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 10);
dispatcherTimer.Start();
}
}
}
private void PlayerToggle()
{
if (Player.playState != WMPPlayState.wmppsPlaying)
{
Player.controls.play();
PathGeometry pg = new PathGeometry();
pg.FillRule = FillRule.Nonzero;
PathFigureCollectionConverter pfcc = new PathFigureCollectionConverter();
pg.Figures = (PathFigureCollection)pfcc.ConvertFrom("M6 19h4V5H6v14zm8-14v14h4V5h-4z");
ToggleButtonPath.Data = pg;
((Path)FindName("PlayTrackButton" + CurrentMediaIndex.ToString() + "Path")).Data = pg;
}
else
{
Player.controls.pause();
PathGeometry pg = new PathGeometry();
pg.FillRule = FillRule.Nonzero;
PathFigureCollectionConverter pfcc = new PathFigureCollectionConverter();
pg.Figures = (PathFigureCollection)pfcc.ConvertFrom("M8 5v14l11-7z");
ToggleButtonPath.Data = pg;
((Path)FindName("PlayTrackButton" + CurrentMediaIndex.ToString() + "Path")).Data = pg;
}
}
private void ToggleButton_Click(object sender, RoutedEventArgs e)
{
if(Playlist.Count > 0)
PlayerToggle();
else
{
PlaylistButton_Click(PlaylistButton, e);
}
}
private void RepeatButton_Click(object sender, RoutedEventArgs e)
{
Button but = sender as Button;
if (!isRepeat && !Player.settings.getMode("loop"))
{
but.MouseLeave -= Button_MouseLeave;
((Path)but.FindName(but.Name + "Path")).Fill = new SolidColorBrush(Color.FromRgb(255, 255, 255));
PathGeometry pg = new PathGeometry();
pg.FillRule = FillRule.Nonzero;
PathFigureCollectionConverter pfcc = new PathFigureCollectionConverter();
pg.Figures = (PathFigureCollection)pfcc.ConvertFrom("M7 7h10v3l4-4-4-4v3H5v6h2V7zm10 10H7v-3l-4 4 4 4v-3h12v-6h-2v4zm-4-2V9h-1l-2 1v1h1.5v4H13z");
((Path)but.FindName(but.Name + "Path")).Data = pg;
Player.settings.setMode("loop", true);
}
else
{
Player.settings.setMode("loop", false);
if (!isRepeat)
{
PathGeometry pg = new PathGeometry();
pg.FillRule = FillRule.Nonzero;
PathFigureCollectionConverter pfcc = new PathFigureCollectionConverter();
pg.Figures = (PathFigureCollection)pfcc.ConvertFrom("M7 7h10v3l4-4-4-4v3H5v6h2V7zm10 10H7v-3l-4 4 4 4v-3h12v-6h-2v4z");
((Path)but.FindName(but.Name + "Path")).Data = pg;
isRepeat = true;
}
else
{
isRepeat = false;
but.MouseLeave += Button_MouseLeave;
((Path)but.FindName(but.Name + "Path")).Fill = new SolidColorBrush(Color.FromRgb(98, 114, 123));
}
}
}
List<Track> Playlist = new List<Track>();
class Track
{
public IWMPMedia Media { get; set; }
public string Title { get; set; }
public string[] Performers { get; set; }
public string Duration { get; set; }
}
private void PlaylistButton_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Multiselect = true;
ofd.DefaultExt = ".mp3";
ofd.Filter = "Audio files (*.mp3)|*.mp3";
ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
if (ofd.ShowDialog() == true)
{
//Playlist.Clear();
foreach (string filename in ofd.FileNames) {
var mp3File = TagLib.File.Create(filename);
Playlist.Add(new Track {
Media = Player.newMedia(filename),
Title = mp3File.Tag.Title,
Performers = mp3File.Tag.Performers,
Duration = mp3File.Properties.Duration.ToString("mm\\:ss")
});
Console.WriteLine("Artist: " + String.Join(", ", mp3File.Tag.Performers));
Console.WriteLine("Track number: " + mp3File.Tag.Track);
Console.WriteLine("Title: " + mp3File.Tag.Title);
Console.WriteLine("Album: " + mp3File.Tag.Album);
Console.WriteLine("Year: " + mp3File.Tag.Year);
Console.WriteLine("Genre: " + mp3File.Tag.FirstGenre);
Console.WriteLine("Bitrate: " + mp3File.Properties.AudioBitrate + " kbps");
Console.WriteLine("Channels: " + mp3File.Properties.AudioChannels);
Console.WriteLine("Duration: " + mp3File.Properties.Duration.ToString("mm\\:ss"));
RowDefinition gridRow = new RowDefinition();
gridRow.Height = new GridLength(50);
Style st = FindResource("PlayerButton") as Style;
PlaylistGrid.RowDefinitions.Add(gridRow);
Button TrackPlayButton = new Button() {
Name = "PlayTrackButton"+ (Playlist.Count - 1).ToString(),
Style = st,
Foreground = new SolidColorBrush(Color.FromRgb(255, 255, 255)),
Background = Brushes.Transparent,
Height = 40,
Width = 40,
Cursor = Cursors.Hand,
HorizontalAlignment = HorizontalAlignment.Left,
Margin = new Thickness(10,0,10,0),
};
TrackPlayButton.MouseEnter += TrackPlayButton_MouseEnter;
TrackPlayButton.MouseLeave += TrackPlayButton_MouseLeave;
TrackPlayButton.Click += TrackPlayButton_Click;
PathGeometry pg = new PathGeometry();
pg.FillRule = FillRule.Nonzero;
PathFigureCollectionConverter pfcc = new PathFigureCollectionConverter();
pg.Figures = (PathFigureCollection)pfcc.ConvertFrom("M8 5v14l11-7z");
Path ButtonPath = new Path()
{
Name = "PlayTrackButton" + (Playlist.Count - 1).ToString()+"Path",
Fill = new SolidColorBrush(Color.FromRgb(255, 255, 255)),
Data = pg,
Stretch = Stretch.Fill,
Height=15,
Width=11,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center
};
Grid PathGrid = new Grid()
{
Width = 10,
Height = 15,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center
};
Label TrackName = new Label()
{
Content = String.Join(", ",mp3File.Tag.Performers)+" - " + mp3File.Tag.Title,
Foreground = Brushes.White,
VerticalAlignment = VerticalAlignment.Center,
Margin = new Thickness(60, 0, 0, 0),
};
PathGrid.Children.Add(ButtonPath);
TrackPlayButton.Content = PathGrid;
Border Container = new Border() {
Height = 50,
Background = Brushes.Transparent,
BorderBrush = new SolidColorBrush(Color.FromRgb(55, 70, 79)),
BorderThickness = new Thickness(0,0,0,2),
Child = TrackPlayButton
};
Grid.SetRow(Container, Playlist.Count - 1);
Grid.SetColumn(Container, 0);
Grid.SetRow(TrackName, Playlist.Count - 1);
Grid.SetColumn(TrackName, 0);
PlaylistGrid.Children.Add(Container);
PlaylistGrid.Children.Add(TrackName);
if (PlaylistGrid.FindName(ButtonPath.Name) == null) { PlaylistGrid.RegisterName(ButtonPath.Name, ButtonPath); }
if (PlaylistScroll.Height < 250) { PlaylistScroll.Height += 50; }
}
}
Player.controls.stop();
if (Playlist.Count > 0)
{
Player.currentMedia = Playlist[0].Media;
CurrentMediaIndex = 0;
PlayerToggle();
}
}
private void TrackPlayButton_Click(object sender, RoutedEventArgs e)
{
Button but = sender as Button; //("M8 5v14l11-7z"); M6 19h4V5H6v14zm8 - 14v14h4V5h - 4z
if (CurrentMediaIndex == int.Parse(but.Name.Substring("PlayTrackButton".Length)))
{
PlayerToggle();
}
else {
PathGeometry pg = new PathGeometry();
pg.FillRule = FillRule.Nonzero;
PathFigureCollectionConverter pfcc = new PathFigureCollectionConverter();
pg.Figures = (PathFigureCollection)pfcc.ConvertFrom("M8 5v14l11-7z");
((Path)FindName("PlayTrackButton" + CurrentMediaIndex.ToString() + "Path")).Data = pg;
Player.controls.stop();
CurrentMediaIndex = int.Parse(but.Name.Substring("PlayTrackButton".Length));
Player.currentMedia = Playlist[CurrentMediaIndex].Media;
PlayerToggle();
}
}
private void TrackPlayButton_MouseLeave(object sender, MouseEventArgs e)
{
Button but = sender as Button;
but.Background = Brushes.Transparent;
((Path)but.FindName(but.Name + "Path")).Fill = Brushes.White;
}
private void TrackPlayButton_MouseEnter(object sender, MouseEventArgs e)
{
Button but = sender as Button;
but.Background = Brushes.White;
((Path)but.FindName(but.Name + "Path")).Fill = new SolidColorBrush(Color.FromRgb(98, 114, 123));
}
private void Button_MouseEnter(object sender, MouseEventArgs e)
{
Button but = sender as Button;
but.Foreground = new SolidColorBrush(Color.FromRgb(255, 255, 255));
((Path)but.FindName(but.Name + "Path")).Fill = new SolidColorBrush(Color.FromRgb(255, 255, 255));
}
private void Button_MouseLeave(object sender, MouseEventArgs e)
{
Button but = sender as Button;
but.Foreground = new SolidColorBrush(Color.FromRgb(98, 114, 123));
((Path)but.FindName(but.Name + "Path")).Fill = new SolidColorBrush(Color.FromRgb(98, 114, 123));
}
private void NextButton_Click(object sender, RoutedEventArgs e)
{
PathGeometry pg = new PathGeometry();
pg.FillRule = FillRule.Nonzero;
PathFigureCollectionConverter pfcc = new PathFigureCollectionConverter();
pg.Figures = (PathFigureCollection)pfcc.ConvertFrom("M8 5v14l11-7z");
((Path)FindName("PlayTrackButton" + CurrentMediaIndex.ToString() + "Path")).Data = pg;
Player.controls.stop();
if (CurrentMediaIndex + 1 == Playlist.Count)
{
CurrentMediaIndex = 0;
Player.URL = Playlist.First().Media.sourceURL;
}
else
{
CurrentMediaIndex++;
Player.URL = Playlist[CurrentMediaIndex].Media.sourceURL;
}
PlayerToggle();
}
private void PrevButton_Click(object sender, RoutedEventArgs e)
{
PathGeometry pg = new PathGeometry();
pg.FillRule = FillRule.Nonzero;
PathFigureCollectionConverter pfcc = new PathFigureCollectionConverter();
pg.Figures = (PathFigureCollection)pfcc.ConvertFrom("M8 5v14l11-7z");
((Path)FindName("PlayTrackButton" + CurrentMediaIndex.ToString() + "Path")).Data = pg;
Player.controls.stop();
if (CurrentMediaIndex == 0)
{
CurrentMediaIndex = Playlist.Count-1;
Player.currentMedia = Playlist.Last().Media;
}
else
{
CurrentMediaIndex--;
Player.currentMedia = Playlist[CurrentMediaIndex].Media;
}
PlayerToggle();
}
private void TopPanel_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left)
DragMove();
}
private void ButtonClose_MouseEnter(object sender, MouseEventArgs e)
{
((Path)FindName("ButtonClosePath")).Fill = Brushes.White;
}
private void ButtonClose_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
System.Windows.Application.Current.Shutdown();
}
}
}