-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMusicPlayerForm.cs
1047 lines (968 loc) · 38.2 KB
/
MusicPlayerForm.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using KControlsLib;
using Null.MciPlayer;
using System;
using System.Reflection;
using System.Security.AccessControl;
using System.Text.RegularExpressions;
namespace MusicPlayer
{
enum PlayMode
{
ListLoop,
Shuffle,
SingleLoop
}
enum CurrentPage
{
Local,
Favourite
}
public partial class MusicPlayerForm : Form
{
private CurrentPage currentPage = CurrentPage.Local;
private PlayMode playMode = PlayMode.ListLoop;
private SongsInfo currSelectedSong = new(null); //用于右键菜单
private MciPlayer? player = null;
private MciPlayer? playerStore = null;
private bool playerStoreValid = false; // 是否已经保存了一个有效的 mci player
private string localSongsFilePath = Application.StartupPath + "\\songListHistory.txt";
private string favoriteSongsFilePath = Application.StartupPath + "\\favoriteSongsHistory.txt";
private List<SongsInfo> searchList = new List<SongsInfo>(); //用于搜索功能
private List<SongsInfo> localSongsList = new List<SongsInfo>(); //本地音乐List
private List<SongsInfo> favoriteSongsList = new List<SongsInfo>(); //收藏音乐List
private int[] randomList = new int[1]; // 随机播放序列
private int randomListIndex = 0; // 序列索引
private int jumpSongIndex; // 手动选中需要在随机过程中跳过的歌曲
private int currentSongTotalMS = 0; // 当前歌曲总长度毫秒,非MCI取得
#region 构造函数、事件绑定
public MusicPlayerForm()
{
InitializeComponent();
this.Load += MusicPlayerForm_Load;
this.Shown += MusicPlayerForm_Shown;
this.FormClosed += this.MusicPlayerForm_FormClosed;
this.btnPrev.ImageNormal = Properties.Resources.prev;
this.btnPrev.ImageDown = Properties.Resources.prevh;
this.btnPrev.HoverText = "上一首";
this.btnPrev.Click += BtnPrev_Click;
this.btnPlayPause.ImageNormal = Properties.Resources.play;
this.btnPlayPause.ImageNormalHover = Properties.Resources.playh;
this.btnPlayPause.ImageDown = Properties.Resources.pause;
this.btnPlayPause.ImageDownHover = Properties.Resources.pauseh;
this.btnPlayPause.HoverText = "播放";
this.btnPlayPause.HoverTextToggle = "暂停";
this.btnPlayPause.ToggleMode = true;
this.btnPlayPause.Click += BtnPlayPause_Click;
this.btnNext.ImageNormal = Properties.Resources.next;
this.btnNext.ImageDown = Properties.Resources.nexth;
this.btnNext.HoverText = "下一首";
this.btnNext.Click += BtnNext_Click;
this.btnLoop.ImageNormal = Properties.Resources.sequence;
this.btnLoop.HoverText = "循环方式";
this.btnLoop.Click += BtnLoop_Click;
this.btnVolume.ImageNormal = Properties.Resources.volume;
this.btnVolume.ImageDown = Properties.Resources.mute;
this.btnVolume.HoverText = "静音";
this.btnVolume.HoverTextToggle = "解除静音";
this.btnVolume.ToggleMode = true;
this.btnVolume.Click += BtnVolume_Click;
this.btnVolume.MouseEnter += BtnVolume_MouseEnter;
this.btnVolume.MouseLeave += BtnVolume_MouseLeave;
this.btnMinium.ImageNormal = Properties.Resources.min;
this.btnMinium.ImageNormalHover = Properties.Resources.minh;
this.btnMinium.HoverText = "最小化";
this.btnMinium.Click += BtnMinium_Click;
this.btnClose.ImageNormal = Properties.Resources.close;
this.btnClose.ImageNormalHover = Properties.Resources.closeh;
this.btnClose.HoverText = "关闭";
this.btnClose.Click += BtnClose_Click;
this.paTitle.MouseDown += PaTitle_MouseDown;
this.paTitle.MouseUp += PaTitle_MouseUp;
this.paTitle.MouseMove += PaTitle_MouseMove;
this.lblTitle.MouseDown += PaTitle_MouseDown;
this.lblTitle.MouseUp += PaTitle_MouseUp;
this.lblTitle.MouseMove += PaTitle_MouseMove;
this.txtSearchSongName.Enter += TxtSearchSongName_Enter;
this.txtSearchSongName.Leave += TxtSearchSongName_Leave;
this.txtSearchSongName.TextChanged += TxtSearchSongName_TextChanged;
SetPropertyInfo(this.lvSongList, "DoubleBuffered", true);
SetPropertyInfo(this.lvSongList, "OptimizedDoubleBuffer ", true);
SetPropertyInfo(this.lvSongList, "AllPaintingInWmPaint", true);
this.lvSongList.DrawColumnHeader += LvSongList_DrawColumnHeader;
this.lvSongList.DrawSubItem += LvSongList_DrawSubItem;
this.lvSongList.DoubleClick += LvSongList_DoubleClick;
this.lvSongList.MouseDown += LvSongList_MouseDown;
// 因为MCI对某些mp3取总长度、取当前位置都有问题,进度条拖动功能关闭
//this.tkbProgress.MouseDown += TkbProgress_MouseDown;
//this.tkbProgress.MouseUp += TkbProgress_MouseUp;
this.tkbVol.MouseHover += TkbVol_MouseHover;
this.tkbVol.MouseLeave += BtnVolume_MouseLeave;
this.tkbVol.Scroll += TkbVol_Scroll;
this.timerPlay.Tick += TimerPlay_Tick;
}
private static void SetPropertyInfo(System.Windows.Forms.Control control, string property, bool value)
{
System.Reflection.PropertyInfo? controlProperty = typeof(System.Windows.Forms.Control)
.GetProperty(property, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
if (controlProperty == null) return;
controlProperty.SetValue(control, value, null);
}
#endregion
#region 外部调用函数
public bool StandAloneMode { get; set; } = true;
public bool Invaded { get; set; } = false;
public void Play(string file, bool repeat = false)
{
player = new MciPlayer(file);
player.Open();
player.Volume(tkbVol.Value);
player.Play(repeat);
}
public void Stop()
{
player?.Stop();
}
public void Pause()
{
player?.Pause();
}
public void Resume()
{
player?.Resume();
}
public void ControlsEnable(bool enable)
{
if (this.InvokeRequired)
{
Action<bool> d = new Action<bool>(ControlsEnable);
this.Invoke(d, enable);
}
else
{
if (enable)
{
// 关闭定时器
this.timerPlay.Stop();
// 禁止按钮、显示Banner
this.tkbProgress.Enabled = false;
this.btnPrev.Enabled = false;
this.btnNext.Enabled = false;
this.btnPlayPause.Enabled = false;
this.lvSongList.DoubleClick -= LvSongList_DoubleClick;
this.lblInvade.Visible = true;
}
else
{
// 允许按钮、隐藏Banner
this.tkbProgress.Enabled = true;
this.btnPrev.Enabled = true;
this.btnNext.Enabled = true;
this.btnPlayPause.Enabled = true;
this.lvSongList.DoubleClick += LvSongList_DoubleClick;
this.lblInvade.Visible = false;
// 仅当之前player有效,才开启定时器
if (playerStoreValid) this.timerPlay.Start();
}
}
}
public void InvadeMode(bool enable)
{
// 操作控件
this.ControlsEnable(enable);
// 外部控制模式开关
if (enable)
{
// 保存player
playerStoreValid = false;
if (player?.GetState() == MciPlayer.PlaybackState.Playing)
{
this.playerStore = player;
player.Pause();
playerStoreValid = true;
}
}
else
{
// 退出Invade模式,先停止当前player
player?.Stop();
player?.Close();
// 如果之前存储player有效,恢复过来,并恢复播放
if (playerStoreValid)
{
this.player = this.playerStore;
this.player?.Resume();
playerStoreValid = false;
}
}
// 标记
this.Invaded = enable;
}
#endregion
#region 标题栏拖动
Point downPoint;
private void PaTitle_MouseMove(object? sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.Location = new Point(this.Location.X + e.X - downPoint.X, this.Location.Y + e.Y - downPoint.Y);
}
}
private void PaTitle_MouseUp(object? sender, MouseEventArgs e)
{
lblTitle.ForeColor = Color.Gray;
}
private void PaTitle_MouseDown(object? sender, MouseEventArgs e)
{
downPoint = new Point(e.X, e.Y);
lblTitle.ForeColor = Color.White;
}
#endregion
#region 顶部菜单
private void lblPlaylist_Click(object sender, EventArgs e)
{
currentPage = CurrentPage.Local;
lblPlaylist.ForeColor = Color.White;
lblFavourite.ForeColor = Color.Gray;
AddSongsToListView(localSongsList);
tsmiFavorite.Visible = true;
}
private void lblFavourite_Click(object sender, EventArgs e)
{
currentPage = CurrentPage.Favourite;
lblPlaylist.ForeColor = Color.Gray;
lblFavourite.ForeColor = Color.White;
AddSongsToListView(favoriteSongsList);
tsmiFavorite.Visible = false;
}
private void lblOpen_Click(object sender, EventArgs e)
{
this.odlgFile.Filter = "媒体文件|*.mp3;*.wav;*.wma;*.avi;*.mpg;*.asf;*.wmv";
this.odlgFile.Multiselect = true;
if (odlgFile.ShowDialog() == DialogResult.OK)
{
for (int i = 0; i < odlgFile.FileNames.Length; i++)
{
string path = odlgFile.FileNames[i];
if (!HavePath(localSongsList, path))
localSongsList.Add(new SongsInfo(path));
}
}
AddSongsToListView(localSongsList);
SaveSongsListHistory(localSongsFilePath, localSongsList);
UpdateSearchList();
}
private void lblClear_Click(object sender, EventArgs e)
{
switch (currentPage)
{
case CurrentPage.Local:
localSongsList.Clear();
SaveSongsListHistory(localSongsFilePath, localSongsList);
AddSongsToListView(localSongsList);
break;
case CurrentPage.Favourite:
favoriteSongsList.Clear();
SaveSongsListHistory(favoriteSongsFilePath, favoriteSongsList);
AddSongsToListView(favoriteSongsList);
break;
}
}
private bool HavePath(List<SongsInfo> songList, string path)
{
for (int i = 0; i < songList.Count; i++)
{
if (path == songList[i].FilePath)
return true;
}
return false;
}
private void AddSongsToListView(List<SongsInfo> songList)
{
lvSongList.BeginUpdate();
lvSongList.Items.Clear();
foreach (SongsInfo song in songList)
{
string[] songAry = new string[6];
int currCount = lvSongList.Items.Count + 1;
if (currCount < 10)
songAry[0] = "0" + currCount;
else
songAry[0] = "" + currCount;
songAry[1] = song.FileName;
songAry[2] = song.Artist;
songAry[3] = song.Album;
songAry[4] = song.Duration;
songAry[5] = song.Filesize;
ListViewItem lvItem = new ListViewItem(songAry);
lvItem.SubItems.Add(song.FilePath);
lvSongList.Items.Add(lvItem);
//WMPLib.IWMPMedia media = AxWmp.newMedia(song.FilePath);
//AxWmp.currentPlaylist.appendItem(media);
}
lvSongList.EndUpdate();
// 不让显示底部Scrollbar,以后再改,kiilii
if (lvSongList.Items.Count > 21)
lvSongList.Columns[0].Width = 62 - SystemInformation.VerticalScrollBarWidth;
else
lvSongList.Columns[0].Width = 62;
}
#endregion
#region 播放列表右键菜单(收藏音乐、删除音乐、打开文件位置)
/*
* 菜单——收藏音乐
*/
private void tsmiFavorite_Click(object sender, EventArgs e)
{
foreach (SongsInfo song in favoriteSongsList)
{
if (currSelectedSong.FilePath == song.FilePath)
return;
}
favoriteSongsList.Add(new SongsInfo(currSelectedSong.FilePath));
SaveSongsListHistory(favoriteSongsFilePath, favoriteSongsList);
}
/*
* 菜单——删除音乐
*/
private void tsmiRemoveSongFromList_Click(object sender, EventArgs e)
{
DeleteSongFormList delForm = new DeleteSongFormList(currSelectedSong.FilePath);
delForm.StartPosition = FormStartPosition.CenterParent;
if (delForm.ShowDialog() == DialogResult.OK)
{
int removeIndex = lvSongList.SelectedItems[0].Index;
if (currentPage == CurrentPage.Local)
{
localSongsList.RemoveAt(removeIndex);
SaveSongsListHistory(localSongsFilePath, localSongsList);
AddSongsToListView(localSongsList);
}
else if (currentPage == CurrentPage.Favourite)
{
favoriteSongsList.RemoveAt(removeIndex);
SaveSongsListHistory(favoriteSongsFilePath, favoriteSongsList);
AddSongsToListView(favoriteSongsList);
}
UpdateSearchList();
}
}
/*
* 菜单——打开文件位置
*/
private void tsmiOpenFilePath_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start(@"Explorer.exe", "/select,\"" + currSelectedSong.FilePath + "\"");
}
#endregion
#region 读写播放列表历史记录
/*
* 保存历史记录到本地文件
*/
private void SaveSongsListHistory(string savePath, List<SongsInfo> songsList)
{
string saveString = "";
for (int i = 0; i < songsList.Count; i++)
{
saveString += songsList[i].FilePath + "};{";
}
File.WriteAllText(savePath, saveString);
}
/*
* 读取历史记录到本地文件
*/
private List<SongsInfo> ReadHistorySongsList(string filePath)
{
List<SongsInfo> resSongList = new List<SongsInfo>();
string readString = "";
if (File.Exists(filePath))
{
readString = File.ReadAllText(filePath);
if (readString != "")
{
string[] arr = readString.Split(new string[] { "};{" }, StringSplitOptions.None);
foreach (string path in arr)
{
if (path != null && path != "" && File.Exists(path))
{
resSongList.Add(new SongsInfo(path));
}
}
}
}
else
File.Create(filePath);
return resSongList;
}
#endregion
#region 播放列表
/*
* 播放列表重绘
*/
private void LvSongList_DrawColumnHeader(object? sender, DrawListViewColumnHeaderEventArgs e)
{
int index = e.ColumnIndex;
e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(27, 27, 25)), e.Bounds);
TextRenderer.DrawText(e.Graphics, lvSongList.Columns[index].Text, new Font("微软雅黑", 9, FontStyle.Regular), e.Bounds, Color.Gray, TextFormatFlags.VerticalCenter | TextFormatFlags.Left);
Pen pen = new Pen(Color.FromArgb(34, 35, 39), 2);
Point p = new Point(e.Bounds.Left - 1, e.Bounds.Top + 1);
Size s = new Size(e.Bounds.Width, e.Bounds.Height - 2);
Rectangle r = new Rectangle(p, s);
e.Graphics.DrawRectangle(pen, r);
}
private void LvSongList_DrawSubItem(object? sender, DrawListViewSubItemEventArgs e)
{
//Console.WriteLine("DrawSub: {0}, state {1}", e.ItemIndex, e.ItemState);
if (e.ItemIndex == -1) return;
// 隔行底色略不同
if (e.ItemIndex % 2 == 0)
{
e.SubItem!.BackColor = Color.FromArgb(27, 29, 32);
e.DrawBackground();
}
// 音乐标题白色,其他灰色
e.SubItem!.ForeColor = e.ColumnIndex == 1 ? Color.White : Color.Gray;
// 选中换底色
if ((e.ItemState & ListViewItemStates.Focused) == ListViewItemStates.Focused)
{
using (SolidBrush brush = new SolidBrush(Color.FromArgb(46, 47, 51)))
{
e.Graphics.FillRectangle(brush, e.Bounds);
}
}
// 绘制文字
if (!string.IsNullOrEmpty(e.SubItem.Text))
{
this.DrawText(e, e.Graphics, e.Bounds, 2);
}
}
private void DrawText(DrawListViewSubItemEventArgs e, Graphics g, Rectangle r, int paddingLeft)
{
TextFormatFlags flags = GetFormatFlags(e.Header!.TextAlign);
r.X += 1 + paddingLeft;//重绘图标时,文本右移
TextRenderer.DrawText(g,
e.SubItem!.Text,
e.SubItem.Font,
r,
e.SubItem.ForeColor,
flags);
}
private TextFormatFlags GetFormatFlags(HorizontalAlignment align)
{
TextFormatFlags flags = TextFormatFlags.EndEllipsis | TextFormatFlags.VerticalCenter;
switch (align)
{
case HorizontalAlignment.Center:
flags |= TextFormatFlags.HorizontalCenter;
break;
case HorizontalAlignment.Right:
flags |= TextFormatFlags.Right;
break;
case HorizontalAlignment.Left:
flags |= TextFormatFlags.Left;
break;
}
return flags;
}
private void UpdateCurrentSongToUI(int index)
{
// 设定ListView
lvSongList.Items[index].Focused = true;
lvSongList.Items[index].EnsureVisible();
lvSongList.Items[index].Selected = true;
lvSongList.Refresh();
// 设定头像、名字等
List<SongsInfo> lst = currentPage switch
{
CurrentPage.Local => localSongsList,
CurrentPage.Favourite => favoriteSongsList,
_ => throw new NotImplementedException(),
};
pbAlbum.Image = lst[index].SmallAblum;
lblArtistName.Text = lst[index].Artist;
lblMusicName.Text = lst[index].OriginName;
// 不用MCI取总长度,VBR的会有问题
var si = new SongsInfo(lvSongList.Items[index].SubItems[6].Text);
Console.WriteLine("Duration {0} bit {1}", si.Duration, si.ByteRate);
var parts = si.Duration.Split(':');
lblEndTime.Text = si.Duration[3..];
currentSongTotalMS = Convert.ToInt32(parts[0]) * 3600000 + Convert.ToInt32(parts[1]) * 60000 + Convert.ToInt32(parts[2]) * 1000;
// 进度条还是用MCI取的值
tkbProgress.Maximum = player!.GetLength();
}
/*
* 播放列表双击事件
*/
private void LvSongList_DoubleClick(object? sender, EventArgs e)
{
Console.WriteLine(lvSongList.SelectedItems[0].Index);
int currIndex = lvSongList.SelectedItems[0].Index;
string songFilePath = lvSongList.Items[currIndex].SubItems[6].Text;
Console.WriteLine("LvSongList_DoubleClick " + songFilePath);
if (player?.DevicePath == songFilePath)
{
//选中歌曲为正在播放的歌曲
this.PlayPauseSwitch();
}
else
{
//选中的为其他歌曲
this.BuildRandomList(lvSongList.Items.Count);
jumpSongIndex = currIndex;
this.PlayNewIndex(currIndex);
this.UpdateCurrentSongToUI(currIndex);
}
}
private void LvSongList_MouseDown(object? sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
ListViewItem lvi = lvSongList.GetItemAt(e.X, e.Y);
if (lvi != null)
{
cmsSongListMenu.Visible = true;
currSelectedSong = new SongsInfo(lvi.SubItems[6].Text);
cmsSongListMenu.Show(Cursor.Position);
}
else
cmsSongListMenu.Close();
}
}
#endregion
#region 列表搜索
private void TxtSearchSongName_Enter(object? sender, EventArgs e)
{
if (txtSearchSongName.Text == "输入要搜索的歌曲名")
txtSearchSongName.Text = "";
}
private void TxtSearchSongName_Leave(object? sender, EventArgs e)
{
if (txtSearchSongName.Text == "")
txtSearchSongName.Text = "输入要搜索的歌曲名";
}
private void TxtSearchSongName_TextChanged(object? sender, EventArgs e)
{
//初始化
if (txtSearchSongName.Text == "输入要搜索的歌曲名")
{
switch (currentPage)
{
case CurrentPage.Local:
AddSongsToListView(localSongsList);
break;
case CurrentPage.Favourite:
AddSongsToListView(favoriteSongsList);
break;
}
return;
}
else if (txtSearchSongName.Text != "")
{
List<SongsInfo> resultList = new List<SongsInfo>();
Dictionary<string, SongsInfo> resultDic = new Dictionary<string, SongsInfo>();
Regex r = new Regex(Regex.Escape(txtSearchSongName.Text), RegexOptions.IgnoreCase);
for (int i = 0; i < localSongsList.Count; i++) // 本地列表内查找
{
Match m = r.Match(localSongsList[i].FileName);
if (m.Success)
{
resultDic.Add(localSongsList[i].FilePath, localSongsList[i]);
}
}
for (int i = 0; i < favoriteSongsList.Count; i++) // 收藏列表内查找
{
Match m = r.Match(favoriteSongsList[i].FileName);
if (m.Success && !resultDic.ContainsKey(favoriteSongsList[i].FilePath))
{
resultDic.Add(favoriteSongsList[i].FilePath, favoriteSongsList[i]);
}
}
if (resultDic.Count > 0) // 如果找到,放入
{
List<SongsInfo> resList = new List<SongsInfo>();
foreach (SongsInfo song in resultDic.Values)
{
resList.Add(song);
}
AddSongsToListView(resList);
}
else
{
lvSongList.Items.Clear();
}
}
}
private void UpdateSearchList()
{
searchList = new List<SongsInfo>();
for (int i = 0; i < lvSongList.Items.Count; i++)
{
searchList.Add(new SongsInfo(lvSongList.Items[i].SubItems[6].Text));
}
}
#endregion
#region 播放核心,MCIPlayer操作
/// <summary>
/// 切换播放状态,如果确实切换了,返回true
/// </summary>
/// <returns></returns>
private bool PlayPauseSwitch()
{
if (player?.GetState() == MciPlayer.PlaybackState.Playing)
{
player.Pause();
timerPlay.Stop();
btnPlayPause.Down = false;
return true;
}
else if (player?.GetState() == MciPlayer.PlaybackState.Paused)
{
player.Resume();
timerPlay.Start();
btnPlayPause.Down = true;
return true;
}
return false;
}
/*
* 播放指定的歌曲
* index:播放列表中,歌曲的序号
*/
private void PlayNewIndex(int index)
{
Console.WriteLine("PlayNewIndex {0}:{1}", index, lvSongList.Items[index].SubItems[6].Text);
string filePath = lvSongList.Items[index].SubItems[6].Text;
// 直接重开播放
player?.Close();
player = new MciPlayer(filePath, this);
player.Open();
player.Volume(tkbVol.Value);
player.Play();
timerPlay.Start();
// 按钮
btnPlayPause.Down = true;
}
/*
* 重置播放器状态信息
*/
private void ReloadStatus()
{
//设置专辑封面为默认
pbAlbum.Image = Properties.Resources.defaultSmallAlbum;
lblMusicName.Text = "未知歌曲";
lblArtistName.Text = "未知艺术家";
lblCurrTime.Text = "00:00";
lblEndTime.Text = "00:00";
tkbVol.Value = tkbVol.Maximum / 2;
tkbProgress.Value = 0;
if (lvSongList.Items.Count > 0 && lvSongList.SelectedItems.Count == 0)
{
lvSongList.Items[0].Selected = true;//设定选中
lvSongList.Items[0].EnsureVisible();//保证可见
lvSongList.Items[0].Focused = true;
}
}
private void EndOfPlayCallback() //播放歌曲结束回调
{
/* 列表没文件直接退 */
if (lvSongList.Items.Count == 0) return;
/* 根据播放文件名计算index */
int currIndex = 0;
for (int i = 0; i < lvSongList.Items.Count; i++)
{
if (player!.DevicePath == lvSongList.Items[i].SubItems[6].Text)
{
currIndex = i;
break;
}
}
/* 根据播放模式选择下一首 */
switch (playMode)
{
case PlayMode.ListLoop:
if (currIndex != lvSongList.Items.Count - 1)
currIndex += 1;
else
currIndex = 0;
break;
case PlayMode.SingleLoop:
//do nothing
break;
case PlayMode.Shuffle:
if (randomList[randomListIndex] == jumpSongIndex) //匹配到需要跳过的歌曲
{
randomListIndex++;
}
if (randomListIndex == randomList.Length)
{
this.BuildRandomList(lvSongList.Items.Count); //重新生成随机序列
jumpSongIndex = -1; //第二轮开始 播放所有歌曲 不跳过
}
currIndex = randomList[randomListIndex++];
if (randomListIndex == randomList.Length)
{
this.BuildRandomList(lvSongList.Items.Count); //重新生成随机序列
jumpSongIndex = -1; //第二轮开始 播放所有歌曲 不跳过
}
break;
}
/* 开始播放 */
this.PlayNewIndex(currIndex);
this.UpdateCurrentSongToUI(currIndex);
}
private void BuildRandomList(int songListCount)
{
randomListIndex = 0;
randomList = new int[songListCount];
//初始化序列
for (int i = 0; i < songListCount; i++)
{
randomList[i] = i;
}
//随机序列
for (int i = songListCount - 1; i >= 0; i--)
{
Random r = new Random(Guid.NewGuid().GetHashCode());
int j = r.Next(0, songListCount - 1);
(randomList[i], randomList[j]) = (randomList[j], randomList[i]);
}
//输出序列
//for (int i = 0; i < songListCount; i++)
//{
// Console.Write(randomList[i] + " ");
//}
//Console.WriteLine(" ");
}
#endregion
#region 按键方法
private void BtnPrev_Click(object? sender, EventArgs e)
{
if (lvSongList.SelectedItems.Count == 0) return;
int currIndex = lvSongList.SelectedItems[0].Index;
if (currIndex > 0)
{
currIndex -= 1;
}
else
{
currIndex = lvSongList.Items.Count - 1;
}
this.PlayNewIndex(currIndex);
this.UpdateCurrentSongToUI(currIndex);
}
private void BtnNext_Click(object? sender, EventArgs e)
{
if (lvSongList.SelectedItems.Count == 0) return;
int currIndex = lvSongList.SelectedItems[0].Index;
if (currIndex < lvSongList.Items.Count - 1)
{
currIndex += 1;
}
else
{
currIndex = 0;
}
this.PlayNewIndex(currIndex);
this.UpdateCurrentSongToUI(currIndex);
}
private void BtnPlayPause_Click(object? sender, EventArgs e)
{
if (lvSongList.Items.Count == 0) return;
// 尝试暂停、恢复;如果成功则退出
if (this.PlayPauseSwitch()) return;
// 当前没有播放,重新开始
// 有选中,按选中的开始; 无选中,按设定播放模式来
int newIndex = 0;
if (lvSongList.SelectedItems.Count > 0)
{
newIndex = lvSongList.SelectedItems[0].Index;
}
switch (playMode)
{
case PlayMode.ListLoop:
case PlayMode.SingleLoop:
this.PlayNewIndex(newIndex);
this.UpdateCurrentSongToUI(newIndex);
break;
case PlayMode.Shuffle:
this.BuildRandomList(lvSongList.Items.Count);
jumpSongIndex = newIndex;
this.PlayNewIndex(randomList[newIndex]);
this.UpdateCurrentSongToUI(randomList[newIndex]);
break;
}
}
private void BtnLoop_Click(object? sender, EventArgs e)
{
switch (playMode)
{
case PlayMode.ListLoop:
this.playMode = PlayMode.Shuffle;
this.btnLoop.ImageNormal = Properties.Resources.random;
break;
case PlayMode.Shuffle:
this.playMode = PlayMode.SingleLoop;
this.btnLoop.ImageNormal = Properties.Resources.repeat;
break;
case PlayMode.SingleLoop:
this.playMode = PlayMode.ListLoop;
this.btnLoop.ImageNormal = Properties.Resources.sequence;
break;
}
}
private void BtnMinium_Click(object? sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}
private void BtnClose_Click(object? sender, EventArgs e)
{
if (StandAloneMode)
{
this.Close();
}
else
{
this.Hide();
}
}
#endregion
#region 播放Trackbar
private void TimerPlay_Tick(object? sender, EventArgs e)
{
//设置当前播放时间
int ms = player!.GetPosition(); // 这个取值又是准确的
lblCurrTime.Text = String.Format("{0:D2}:{1:D2}", ms / 60000, (ms / 1000) % 60);
//设置滑动条值,采用妥协方式
double corrected = ms;
corrected = corrected * tkbProgress.Maximum / currentSongTotalMS;
if ((int)corrected <= tkbProgress.Maximum)
tkbProgress.Value = (int)corrected;
}
private void TkbProgress_MouseDown(object? sender, EventArgs e)
{
timerPlay.Stop();
}
private void TkbProgress_MouseUp(object? sender, EventArgs e)
{
if (player == null) return;
switch (player?.GetState())
{
case MciPlayer.PlaybackState.Playing:
player?.Seek(tkbProgress.Value);
player?.Play();
timerPlay.Start();
break;
case MciPlayer.PlaybackState.Stopped:
case MciPlayer.PlaybackState.Paused:
player?.Seek(tkbProgress.Value);
this.btnPlayPause.Down = false;
break;
}
}
#endregion
#region 音量控制按钮和Trackbar
private int preVolume = 500;
private void BtnVolume_Click(object? sender, EventArgs e)
{
Console.WriteLine("btnVolume_Click");
if (btnVolume.Down)
{
preVolume = tkbVol.Value;
tkbVol.Value = tkbVol.Minimum;
}
else
{
tkbVol.Value = preVolume;
}
player?.Volume(tkbVol.Value);
}
private void BtnVolume_MouseEnter(object? sender, EventArgs e)
{
this.tkbVol.Visible = true;
this.tkbVol.BringToFront();
}
private void BtnVolume_MouseLeave(object? sender, EventArgs e)
{
Rectangle r = this.tkbVol.Bounds;
r.Height += 30; // 两个控件之间有点距离
if (!r.Contains(this.PointToClient(Control.MousePosition)))
this.tkbVol.Visible = false;
}
private void TkbVol_Scroll(object? sender, EventArgs e)
{
if (tkbVol.Value == tkbVol.Minimum) // 拖到最低改变状态
{
btnVolume.Down = true;
}
else if (btnVolume.Down) // 静音状态移动则解除静音
{
btnVolume.Down = false;
}
player?.Volume(tkbVol.Value);
}
private void TkbVol_MouseHover(object? sender, EventArgs e)
{
new ToolTip().SetToolTip(this, $"音量:{tkbVol.Value}");
}
#endregion
#region 窗体Notify、Load、Shown、Closed事件
protected override void DefWndProc(ref Message m)
{
switch (m.Msg)
{
case MciEventCode.MM_MCINOTIFY:
if (m.WParam.ToInt32() == MciEventCode.MCI_NOTIFY_SUCCESSFUL)
{
this.EndOfPlayCallback();
}
Console.WriteLine("MM_MCINOTIFY " + m.WParam + " " + m.LParam);
break;
}
base.DefWndProc(ref m);
}
/*
* 设置各种初始状态
*/
private void MusicPlayerForm_Load(object? sender, EventArgs e)
{