MainForm.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. using NAudio.Wave;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Net;
  10. using System.Reflection;
  11. using System.Text;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. using System.Windows.Forms;
  15. namespace MusicPlayer
  16. {
  17. public partial class MainForm : Form
  18. {
  19. bool songFinished;
  20. bool draggedstarted = false;
  21. bool draggedcompleted = false;
  22. int startx = 0;
  23. int starty = 0;
  24. public Main main
  25. {
  26. get; set;
  27. }
  28. public MainForm()
  29. {
  30. InitializeComponent();
  31. PositionTrackBar.Scroll += (s, e) =>
  32. {
  33. this.PositionTrackBar_ValueChanged();
  34. };
  35. PositionTrackBar.MouseDown += (s, e) =>
  36. {
  37. clicked = true;
  38. };
  39. PositionTrackBar.MouseUp += (s, e) =>
  40. {
  41. if (!clicked)
  42. return;
  43. clicked = false;
  44. this.PositionTrackBar_ValueChanged();
  45. };
  46. songFinished = false;
  47. }
  48. private void MainForm_Load(object sender, EventArgs e)
  49. {
  50. UpdateTimer.Start();
  51. }
  52. private void PlayButton_Click(object sender, EventArgs e)
  53. {
  54. if (main.audio.AState == AudioHandler.AudioState.PAUSED)
  55. main.audio.Play(main.audio.CurrentSong);
  56. else
  57. SongsTableView_CellDoubleClick(sender, new DataGridViewCellEventArgs(0, 0));
  58. }
  59. private void PauseButton_Click(object sender, EventArgs e)
  60. {
  61. main.audio.Pause();
  62. }
  63. private void StopButton_Click(object sender, EventArgs e)
  64. {
  65. main.audio.Stop();
  66. }
  67. public void SongFinished()
  68. {
  69. songFinished = true;
  70. }
  71. private void UpdateTimer_Tick(object sender, EventArgs e)
  72. {
  73. //Trackbar
  74. if (main.audio.BState == AudioHandler.BufferState.DONE)
  75. PositionTrackBar.Enabled = true;
  76. else
  77. PositionTrackBar.Enabled = false;
  78. if (!clicked)
  79. PositionTrackBar.Value = Math.Max(main.audio.Position, 0);
  80. //Buffer display
  81. BufferBar.Value = main.audio.Buffered / 10;
  82. //Time labels
  83. if (!clicked)
  84. LabelCurrentTime.Text = Main.SecondsToTimestamp(main.audio.CurrentTime);
  85. LabelTotalTime.Text = Main.SecondsToTimestamp(main.audio.TotalTime);
  86. //Current song label
  87. if (main.audio.CurrentSong == null)
  88. CurrentSongLabel.Text = "Not playing any songs";
  89. else
  90. {
  91. if (main.audio.CurrentSong.Seconds < 1)
  92. PositionTrackBar.Enabled = false;
  93. CurrentSongLabel.Text = "Currently playing: " + main.audio.CurrentSong.Name;
  94. }
  95. //Buttons and context menu
  96. if (main.audio.AState == AudioHandler.AudioState.PLAYING)
  97. {
  98. PlayButton.Enabled = false;
  99. NotifyMenuStripPlayButton.Enabled = false;
  100. NotifyMenuStripPlayingLabel.Text = "Playing";
  101. NotifyMenuStripPlayingLabel.Enabled = true;
  102. NotifyMenuStripPlayingSongLabel.Visible = true;
  103. NotifyMenuStripPlayingSongLabel.Text = main.audio.CurrentSong.Name;
  104. }
  105. else
  106. {
  107. PlayButton.Enabled = true;
  108. NotifyMenuStripPlayButton.Enabled = true;
  109. NotifyMenuStripPlayingSongLabel.Visible = false;
  110. }
  111. if (main.nw.ip == "http://jancokock.me")
  112. SelectServerJancoButton.Enabled = false;
  113. else
  114. SelectServerJancoButton.Enabled = true;
  115. if (main.nw.ip == "http://imegumii.nl")
  116. SelectServerYorickButton.Enabled = false;
  117. else
  118. SelectServerYorickButton.Enabled = true;
  119. if (main.audio.AState == AudioHandler.AudioState.PAUSED)
  120. {
  121. PauseButton.Enabled = false;
  122. NotifyMenuStripPauseButton.Enabled = false;
  123. NotifyMenuStripPlayingLabel.Text = "Paused";
  124. NotifyMenuStripPlayingLabel.Enabled = true;
  125. NotifyMenuStripPlayingSongLabel.Visible = true;
  126. NotifyMenuStripPlayingSongLabel.Text = main.audio.CurrentSong.Name;
  127. }
  128. else
  129. {
  130. PauseButton.Enabled = true;
  131. NotifyMenuStripPauseButton.Enabled = true;
  132. }
  133. if (main.audio.AState == AudioHandler.AudioState.STOPPED)
  134. {
  135. StopButton.Enabled = false;
  136. NotifyMenuStripStopButton.Enabled = false;
  137. NotifyMenuStripPlayingLabel.Text = "Stopped";
  138. NotifyMenuStripPlayingLabel.Enabled = false;
  139. NotifyMenuStripPlayingSongLabel.Visible = false;
  140. NotifyMenuStripPlayingSongLabel.Text = "Stopped";
  141. }
  142. else
  143. {
  144. StopButton.Enabled = true;
  145. NotifyMenuStripStopButton.Enabled = true;
  146. }
  147. if(PlayNextSongButton.Checked)
  148. {
  149. ShuffleSongButton.Enabled = true;
  150. }
  151. else
  152. {
  153. ShuffleSongButton.Enabled = false;
  154. ShuffleSongButton.Checked = false;
  155. }
  156. if (main.currentPlayingList.Count <= 1)
  157. {
  158. PreviousButton.Enabled = false;
  159. NextButton.Enabled = false;
  160. NotifyMenuStripPreviousButton.Enabled = false;
  161. NotifyMenuStripNextButton.Enabled = false;
  162. }
  163. else
  164. {
  165. NextButton.Enabled = true;
  166. NotifyMenuStripNextButton.Enabled = true;
  167. if (ShuffleSongButton.Checked)
  168. {
  169. PreviousButton.Enabled = false;
  170. NotifyMenuStripPreviousButton.Enabled = false;
  171. }
  172. else
  173. {
  174. PreviousButton.Enabled = true;
  175. NotifyMenuStripPreviousButton.Enabled = true;
  176. }
  177. }
  178. if (songFinished)
  179. {
  180. Thread.Sleep(20);
  181. if (PlayNextSongButton.Checked)
  182. {
  183. NextButton_Click(this, new EventArgs());
  184. }
  185. else if (LoopSongButton.Checked)
  186. {
  187. main.audio.Play(main.audio.CurrentSong);
  188. }
  189. songFinished = false;
  190. }
  191. }
  192. private void GenreListBox_SelectedIndexChanged(object sender, EventArgs e)
  193. {
  194. if (GenreListBox.SelectedItems.Count != 0)
  195. {
  196. main.GenreFilter(GenreListBox.SelectedItems[0].ToString());
  197. ArtistListBox.ClearSelected();
  198. PlaylistBox.ClearSelected();
  199. AlbumListView.SelectedIndices.Clear();
  200. }
  201. }
  202. private void PlaylistBox_SelectedIndexChanged(object sender, EventArgs e)
  203. {
  204. if (PlaylistBox.SelectedItems.Count != 0)
  205. {
  206. main.PlaylistFilter(PlaylistBox.SelectedItems[0].ToString());
  207. GenreListBox.ClearSelected();
  208. ArtistListBox.ClearSelected();
  209. AlbumListView.SelectedIndices.Clear();
  210. }
  211. }
  212. private void ArtistListBox_SelectedIndexChanged(object sender, EventArgs e)
  213. {
  214. if (ArtistListBox.SelectedItems.Count != 0)
  215. {
  216. main.ArtistFilter(ArtistListBox.SelectedItems[0].ToString());
  217. GenreListBox.ClearSelected();
  218. PlaylistBox.ClearSelected();
  219. AlbumListView.SelectedIndices.Clear();
  220. }
  221. }
  222. private void AlbumListView_SelectedIndexChanged(object sender, EventArgs e)
  223. {
  224. if (AlbumListView.SelectedItems.Count != 0)
  225. {
  226. main.AlbumFilter(AlbumListView.SelectedItems[0].Text);
  227. PlaylistBox.ClearSelected();
  228. ArtistListBox.ClearSelected();
  229. GenreListBox.ClearSelected();
  230. }
  231. }
  232. private void SongsTableView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
  233. {
  234. if (e.RowIndex != -1)
  235. {
  236. SongsTable s = new SongsTable();
  237. if (SongsTableView.SelectedRows.Count > 0)
  238. {
  239. main.currentPlayingList = main.table.AsEnumerable().Select(x => x[5] as Song).ToList();
  240. var drv = SongsTableView.SelectedRows[0].DataBoundItem as DataRowView;
  241. var row = drv.Row as DataRow;
  242. s.ImportRow(row);
  243. main.audio.Play((s.Rows[0][5] as Song));
  244. }
  245. }
  246. }
  247. private void PositionTrackBar_ValueChanged()
  248. {
  249. if (!clicked)
  250. main.audio.Seek(PositionTrackBar.Value);
  251. LabelCurrentTime.Text = Main.SecondsToTimestamp((int)(((double)PositionTrackBar.Value / 1000) * main.audio.CurrentSong.Seconds));
  252. }
  253. private void NotifyIcon_Click(object sender, MouseEventArgs e)
  254. {
  255. if (e.Button == MouseButtons.Left)
  256. {
  257. MethodInfo mi = typeof(NotifyIcon).GetMethod("ShowContextMenu", BindingFlags.Instance | BindingFlags.NonPublic);
  258. mi.Invoke(NotifyIcon, null);
  259. }
  260. }
  261. private void overviewToolStripMenuItem_Click(object sender, EventArgs e)
  262. {
  263. this.PlaylistBox.Visible = false;
  264. this.GenreListBox.Visible = true;
  265. this.ArtistListBox.Visible = true;
  266. this.AlbumListView.Visible = true;
  267. this.GenreListLabel.Visible = true;
  268. this.AlbumListLabel.Visible = true;
  269. this.ArtistListLabel.Visible = true;
  270. this.PlaylistListLabel.Visible = false;
  271. }
  272. private void playlistsToolStripMenuItem_Click(object sender, EventArgs e)
  273. {
  274. this.PlaylistBox.Visible = true;
  275. this.GenreListBox.Visible = false;
  276. this.ArtistListBox.Visible = false;
  277. this.AlbumListView.Visible = false;
  278. this.GenreListLabel.Visible = false;
  279. this.AlbumListLabel.Visible = false;
  280. this.ArtistListLabel.Visible = false;
  281. this.PlaylistListLabel.Visible = true;
  282. }
  283. private void exitToolStripMenuItem_Click(object sender, EventArgs e)
  284. {
  285. ExitProgram();
  286. }
  287. private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
  288. {
  289. ExitProgram();
  290. }
  291. private void ExitProgram()
  292. {
  293. main.audio.AState = AudioHandler.AudioState.STOPPED;
  294. NotifyIcon.Visible = false;
  295. NotifyIcon.Icon = null;
  296. System.Windows.Forms.Application.Exit();
  297. }
  298. private void NotifyMenuStripPlayButton_Click(object sender, EventArgs e)
  299. {
  300. PlayButton_Click(sender, e);
  301. }
  302. private void NotifyMenuStripPauseButton_Click(object sender, EventArgs e)
  303. {
  304. PauseButton_Click(sender, e);
  305. }
  306. private void NotifyMenuStripStopButton_Click(object sender, EventArgs e)
  307. {
  308. StopButton_Click(sender, e);
  309. }
  310. private void makeToolStripMenuItem_Click(object sender, EventArgs e)
  311. {
  312. PlaylistMaker p = new PlaylistMaker(main.pl, main.api);
  313. p.ShowDialog();
  314. main.Repopulate();
  315. }
  316. private void SearchArtistsTextBox_KeyUp(object sender, KeyEventArgs e)
  317. {
  318. main.SearchArtist(SearchArtistsTextBox.Text);
  319. if (SearchArtistsTextBox.Text.Length < 1)
  320. ClearArtistSearchButton.Enabled = false;
  321. else
  322. ClearArtistSearchButton.Enabled = true;
  323. }
  324. private void SearchGenresTextBox_KeyUp(object sender, KeyEventArgs e)
  325. {
  326. main.SearchGenre(SearchGenresTextBox.Text);
  327. if (SearchGenresTextBox.Text.Length < 1)
  328. ClearGenreSearchButton.Enabled = false;
  329. else
  330. ClearGenreSearchButton.Enabled = true;
  331. }
  332. private void ClearArtistSearchButton_Click(object sender, EventArgs e)
  333. {
  334. main.SearchArtist("");
  335. SearchArtistsTextBox.Text = "";
  336. ClearArtistSearchButton.Enabled = false;
  337. }
  338. private void ClearGenreSearchButton_Click(object sender, EventArgs e)
  339. {
  340. main.SearchGenre("");
  341. SearchGenresTextBox.Text = "";
  342. ClearGenreSearchButton.Enabled = false;
  343. }
  344. private void PositionTrackBar_MouseDown(object sender, MouseEventArgs e)
  345. {
  346. double dblValue;
  347. dblValue = ((double)(e.X+PositionTrackBar.Location.X) / (double)(PositionTrackBar.Width + PositionTrackBar.Location.X)) * (PositionTrackBar.Maximum - PositionTrackBar.Minimum);
  348. PositionTrackBar.Value = Convert.ToInt32(dblValue);
  349. }
  350. private void PreviousButton_Click(object sender, EventArgs e)
  351. {
  352. int selected = 0;
  353. selected = main.currentPlayingList.IndexOf(main.audio.CurrentSong) - 1;
  354. if (selected < 0)
  355. {
  356. if (LoopSongButton.Checked)
  357. selected = main.currentPlayingList.Count-1;
  358. }
  359. main.FilterCurrentPlaying();
  360. SongsTableView.CurrentCell = SongsTableView.Rows[selected].Cells[0];
  361. main.audio.Play(main.currentPlayingList[selected]);
  362. }
  363. private void NextButton_Click(object sender, EventArgs e)
  364. {
  365. int selected = 0;
  366. if (ShuffleSongButton.Checked)
  367. {
  368. Random r = new Random();
  369. selected = r.Next(0, main.currentPlayingList.Count);
  370. while (selected == main.currentPlayingList.IndexOf(main.audio.CurrentSong) && main.currentPlayingList.Count > 1)
  371. selected = r.Next(0, main.currentPlayingList.Count);
  372. }
  373. else
  374. selected = main.currentPlayingList.IndexOf(main.audio.CurrentSong) + 1;
  375. if (selected >= main.currentPlayingList.Count)
  376. {
  377. if (LoopSongButton.Checked)
  378. selected = 0;
  379. else
  380. {
  381. songFinished = false;
  382. return;
  383. }
  384. }
  385. main.FilterCurrentPlaying();
  386. SongsTableView.CurrentCell = SongsTableView.Rows[selected].Cells[0];
  387. main.audio.Play(main.currentPlayingList[selected]);
  388. }
  389. private void NotifyMenuStripNextButton_Click(object sender, EventArgs e)
  390. {
  391. NextButton_Click(sender, e);
  392. }
  393. private void NotifyMenuStripPreviousButton_Click(object sender, EventArgs e)
  394. {
  395. PreviousButton_Click(sender, e);
  396. }
  397. private void ViewCurrentPlaylistButton_Click(object sender, EventArgs e)
  398. {
  399. main.FilterCurrentPlaying();
  400. int selected = main.currentPlayingList.IndexOf(main.audio.CurrentSong);
  401. if(main.currentPlayingList.Count >= 1)
  402. SongsTableView.CurrentCell = SongsTableView.Rows[selected].Cells[0];
  403. }
  404. private void SongsTableView_MouseDown(object sender, MouseEventArgs e)
  405. {
  406. draggedstarted = true;
  407. draggedcompleted = false;
  408. startx = e.X;
  409. starty = e.Y;
  410. }
  411. private void SongsTableView_MouseUp(object sender, MouseEventArgs e)
  412. {
  413. if (draggedcompleted)
  414. {
  415. Cursor.Current = Cursors.Default;
  416. Point point = PlaylistBox.PointToClient(Cursor.Position);
  417. int index = PlaylistBox.IndexFromPoint(point);
  418. if (index < 0) //nope, niet op een playlist gesleept
  419. {
  420. draggedstarted = false;
  421. draggedcompleted = false;
  422. return;
  423. }
  424. Playlist currentPlaylist = main.pl.GetPlaylistByName(PlaylistBox.Items[index].ToString());
  425. SongsTable s = new SongsTable();
  426. if (SongsTableView.SelectedRows.Count > 0)
  427. {
  428. var drv = SongsTableView.SelectedRows[0].DataBoundItem as DataRowView;
  429. var row = drv.Row as DataRow;
  430. s.ImportRow(row);
  431. currentPlaylist.AddSong((s.Rows[0][5] as Song));
  432. currentPlaylist.WriteToFile();
  433. }
  434. }
  435. draggedcompleted = false;
  436. draggedstarted = false;
  437. }
  438. private void SongsTableView_MouseMove(object sender, MouseEventArgs e)
  439. {
  440. int deltax = Math.Abs(startx - e.X);
  441. int deltay = Math.Abs(starty - e.Y);
  442. if ((deltax > 5 || deltay > 5) && draggedstarted && !draggedcompleted)
  443. {
  444. draggedcompleted = true;
  445. playlistsToolStripMenuItem_Click(this, new EventArgs());
  446. Cursor.Current = Cursors.Hand;
  447. }
  448. }
  449. private void SelectServerJancoButton_Click(object sender, EventArgs e)
  450. {
  451. main.SwitchServer("http://jancokock.me");
  452. }
  453. private void SelectServerYorickButton_Click(object sender, EventArgs e)
  454. {
  455. main.SwitchServer("http://imegumii.nl");
  456. }
  457. }
  458. }