Advertisement
Guest User

Untitled

a guest
Jun 2nd, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 36.63 KB | None | 0 0
  1. #region References
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using System.IO;
  12. using System.Media;
  13. using WMPLib;
  14. using System.Runtime.InteropServices;
  15. using System.Diagnostics;
  16. using System.Windows.Input;
  17. using System.Threading;
  18. using Utilities;
  19. using YoutubeExtractor;
  20. using Newtonsoft;
  21. using Microsoft.VisualBasic;
  22. using MediaToolkit;
  23. using VideoLibrary;
  24. #endregion
  25. namespace RohuPlayer
  26. {
  27.  
  28. public partial class Form1 : Form
  29. {
  30.  
  31. #region Objects and Arrays
  32. //Key detection
  33. globalKeyboardHook gkh = new globalKeyboardHook();
  34. //MEDIA PLAYER
  35. AxWMPLib.AxWindowsMediaPlayer player;
  36. //LABEL ARRAY
  37. Button[] buttons;
  38. //Song array cointaining all the songs in the program
  39. public Song[] songs;
  40. //Shuffle object
  41. Shuffle shuffle;
  42. #endregion
  43.  
  44. #region Variables
  45. //What song is selected
  46. int selectedIndex = 0;
  47. //Next pos for button
  48. int nextLabelY;
  49. int nextLabelX;
  50. //If previous was pressed twice;
  51. int doublePrevClicks = 0;
  52. //If song is paused
  53. bool paused = false;
  54. //If Shuffle is activated
  55. bool shuffleActivated = false;
  56. //Current Song that is playing
  57. public int playing;
  58. //index for songs
  59. public int index = 0;
  60. //Download
  61. int downloadClicked = 0;
  62. #endregion
  63.  
  64. #region Constructor
  65. public Form1()
  66. {
  67. InitializeComponent();
  68. #region Keyboard Hook
  69. //Set up keybinds
  70. //play
  71. gkh.HookedKeys.Add(Keys.F5);
  72. //pause
  73. gkh.HookedKeys.Add(Keys.F6);
  74. //next
  75. gkh.HookedKeys.Add(Keys.F7);
  76. //previous
  77. gkh.HookedKeys.Add(Keys.F8);
  78. //Plus 10 sec
  79. gkh.HookedKeys.Add(Keys.F9);
  80. //Minus 10 sec
  81. gkh.HookedKeys.Add(Keys.F10);
  82. //Plus 60 sec
  83. gkh.HookedKeys.Add(Keys.F11);
  84. //Minus 60 sec
  85. gkh.HookedKeys.Add(Keys.F12);
  86. //minus volume
  87. gkh.HookedKeys.Add(Keys.F3);
  88. //plus volume
  89. gkh.HookedKeys.Add(Keys.F4);
  90.  
  91.  
  92.  
  93. gkh.KeyDown += new KeyEventHandler(gkh_KeyDown);
  94.  
  95. gkh.KeyUp += new KeyEventHandler(gkh_KeyUp);
  96.  
  97. #endregion
  98.  
  99. #region Variable Initializing
  100. nextLabelY = 13;
  101. nextLabelX = 13;
  102. //Create song array
  103. songs = new Song[128];
  104. //Initialize media player
  105. player = new AxWMPLib.AxWindowsMediaPlayer();
  106. //Create controls for media player
  107. player.CreateControl();
  108. //Set the current volume to the programs volume bar
  109. trackBar1.Value = player.settings.volume;
  110. //Init labels array
  111. buttons = new Button[128];
  112. #endregion
  113.  
  114. #region File reading
  115. ReadFile();
  116. #endregion
  117.  
  118. #region Buttons
  119. CreateSongListOS();
  120. //Buttons that are out of a certain area are made invisible
  121. ButtonVisibility();
  122. #endregion
  123.  
  124. }
  125. #endregion
  126.  
  127. #region Keyboard Hook Functions
  128. void gkh_KeyUp(object sender, KeyEventArgs e)
  129. {
  130. e.Handled = true;
  131. }
  132.  
  133. void gkh_KeyDown(object sender, KeyEventArgs e)
  134. {
  135. switch (e.KeyCode)
  136. {
  137. case Keys.F3:
  138. if (player.currentMedia != null)
  139. {
  140. if (player.settings.volume - 2 >= 0)
  141. {
  142. player.settings.volume -= 2;
  143. trackBar1.Value = player.settings.volume;
  144. }
  145. }
  146. break;
  147. case Keys.F4:
  148. if (player.currentMedia != null)
  149. {
  150. if (player.settings.volume + 2 <= 100)
  151. {
  152. player.settings.volume += 2;
  153. trackBar1.Value = player.settings.volume;
  154. }
  155. }
  156. break;
  157. case Keys.F5:
  158. //Play
  159. button2.PerformClick();
  160. break;
  161. case Keys.F6:
  162. //Pause
  163. button4.PerformClick();
  164. break;
  165. case Keys.F7:
  166. //Previous song
  167. button9.PerformClick();
  168. break;
  169. case Keys.F8:
  170. //Next song
  171. button8.PerformClick();
  172. break;
  173. case Keys.F9:
  174. //plus 10
  175. button3.PerformClick();
  176. break;
  177. case Keys.F10:
  178. //minus 10
  179. button5.PerformClick();
  180. break;
  181. case Keys.F11:
  182. //plus 60
  183. button11.PerformClick();
  184. break;
  185. case Keys.F12:
  186. //minus 60
  187. button10.PerformClick();
  188. break;
  189. }
  190.  
  191. e.Handled = true;
  192. }
  193. #endregion
  194.  
  195. #region Run-Time Button
  196. private void songButton_Click(object sender, EventArgs e)
  197. {
  198. int index = 0;
  199. foreach (Button b in buttons)
  200. {
  201. if (sender == buttons[index])
  202. {
  203. selectedIndex = index;
  204.  
  205. }
  206. index++;
  207. }
  208. }
  209. #endregion
  210.  
  211. #region Control Buttons
  212. //Add Song
  213. private void button1_Click(object sender, EventArgs e)
  214. {
  215. //If the song is already in the list
  216. bool copy = false;
  217. //Make new song
  218. Song song = new Song();
  219. //For every song check if there is a duplicate
  220. copy = CheckDuplicate(song);
  221.  
  222. if (song.success)
  223. {
  224. songs[index] = song;
  225. index++;
  226. Reload();
  227. }
  228. //If its a duplicate
  229. if (copy)
  230. {
  231. //Get the song to remove
  232. int songToRemove = -1;
  233. for (int i = 0; i < songs.Length; i++)
  234. {
  235. if (buttons[i] == null)
  236. {
  237. songToRemove = i - 1;
  238. break;
  239. }
  240. }
  241. //Remove Buttons
  242. RemoveButtons();
  243.  
  244. //Remove The song
  245. RemoveSong(songToRemove);
  246. //Create a new list of songs on screen
  247. CreateSongListOS();
  248.  
  249. //Highlight Selected Index
  250. HighLightText(selectedIndex);
  251. //Reload screen
  252. Reload();
  253. }
  254.  
  255.  
  256. }
  257.  
  258. //Remove Song
  259. private void button6_Click(object sender, EventArgs e)
  260. {
  261.  
  262. //Song index to remove
  263. int songToRemove = selectedIndex;
  264.  
  265. //Double Check
  266. DialogResult dialogResult = MessageBox.Show("Do you want to remove the song: " + songs[songToRemove].name, "Remove Song?", MessageBoxButtons.YesNo);
  267.  
  268. if (dialogResult == DialogResult.Yes)
  269. {
  270. //Remove song buttons
  271. RemoveButtons();
  272. //Remove The song
  273. ReWriteFile(songToRemove);
  274. //Reload Buttons
  275. CreateSongListOS();
  276. //Stop current song
  277. player.Ctlcontrols.stop();
  278. player.URL = songs[0].directory;
  279. //Reload
  280. Reload();
  281.  
  282. }
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290. }
  291.  
  292. //Play Button
  293. private void button2_Click(object sender, EventArgs e)
  294. {
  295. if (paused)
  296. {
  297. player.Ctlcontrols.play();
  298. paused = false;
  299. return;
  300. }
  301.  
  302. if (selectedIndex <= songs.Length && selectedIndex >= 0 && songs[selectedIndex] != null)
  303. {
  304. HighLightText(selectedIndex);
  305. //Set new song up
  306. player.URL = songs[selectedIndex].directory;
  307. playing = selectedIndex;
  308. //Start the current second timer.
  309. timer2.Start();
  310. //Delay timer1 for 2 sec, because it does not work otherwise.
  311. timer3.Start();
  312. //Play
  313. player.Ctlcontrols.play();
  314. }
  315. else
  316. {
  317. player.Ctlcontrols.play();
  318. }
  319.  
  320. }
  321.  
  322. //Pause Button
  323. private void button4_Click(object sender, EventArgs e)
  324. {
  325. if(player.currentMedia != null)
  326. {
  327. paused = true;
  328. player.Ctlcontrols.pause();
  329. }
  330.  
  331. }
  332.  
  333. //+10 seconds
  334. private void button5_Click(object sender, EventArgs e)
  335. {
  336. if (player.currentMedia != null && player.Ctlcontrols.currentPosition + 10 < player.currentMedia.duration)
  337. {
  338. player.Ctlcontrols.currentPosition += 10;
  339. }
  340. }
  341.  
  342. //-10 seconds
  343. private void button3_Click(object sender, EventArgs e)
  344. {
  345. if (player.currentMedia != null && player.Ctlcontrols.currentPosition - 10 > 0)
  346. {
  347. player.Ctlcontrols.currentPosition -= 10;
  348. }
  349. else if (player.currentMedia != null && (int)player.Ctlcontrols.currentPosition <= 10)
  350. {
  351. player.Ctlcontrols.currentPosition = 0;
  352. }
  353. }
  354.  
  355. //Shuffle songs
  356. private void button7_Click(object sender, EventArgs e)
  357. {
  358.  
  359. shuffleActivated = !shuffleActivated;
  360. shuffle = new Shuffle(ref songs);
  361. if (shuffleActivated)
  362. {
  363. //Stop Current Song
  364. player.Ctlcontrols.stop();
  365. //Set selected index to shuffle objects current song
  366. selectedIndex = shuffle.currentSong;
  367. //Update song URL
  368. player.URL = songs[shuffle.currentSong].directory;
  369. //Start Timers
  370. timer2.Start();
  371. //Delay timer1 for 2 sec, because it does not work otherwise.
  372. timer3.Start();
  373. timer4.Start();
  374. //Highlight Current Song
  375. HighLightText(selectedIndex);
  376. }
  377.  
  378.  
  379.  
  380. }
  381.  
  382. //Next Song
  383. private void button8_Click(object sender, EventArgs e)
  384. {
  385.  
  386. playing++;
  387.  
  388. if (shuffleActivated)
  389. {
  390. shuffle.Next(ref player);
  391. player.URL = songs[shuffle.currentSong].directory;
  392. player.Ctlcontrols.play();
  393. HighLightText(shuffle.currentSong);
  394. return;
  395. }
  396.  
  397. if (songs[playing] == null)
  398. {
  399. playing--;
  400. }
  401. else
  402. {
  403. if(playing > 8)
  404. {
  405. button13.PerformClick();
  406. }
  407. player.URL = songs[playing].directory;
  408. player.Ctlcontrols.play();
  409.  
  410. }
  411. HighLightText(playing);
  412. }
  413.  
  414. //Previous Song
  415. private void button9_Click(object sender, EventArgs e)
  416. {
  417. doublePrevClicks++;
  418. if (doublePrevClicks >= 2)
  419. {
  420. playing--;
  421. if (playing < 0)
  422. {
  423. playing = 0;
  424. }
  425. else
  426. {
  427. button12.PerformClick();
  428. player.URL = songs[playing].directory;
  429. player.Ctlcontrols.play();
  430. doublePrevClicks = 0;
  431. }
  432. }
  433. else
  434. {
  435. player.Ctlcontrols.stop();
  436. player.Ctlcontrols.play();
  437. }
  438. HighLightText(playing);
  439.  
  440. }
  441.  
  442.  
  443. //+60 seconds
  444. private void button10_Click(object sender, EventArgs e)
  445. {
  446. if (player.currentMedia != null && player.Ctlcontrols.currentPosition + 60 < player.currentMedia.duration)
  447. {
  448. player.Ctlcontrols.currentPosition += 60;
  449. }
  450. }
  451.  
  452. //-60 seconds
  453. private void button11_Click(object sender, EventArgs e)
  454. {
  455. if (player.currentMedia != null && player.Ctlcontrols.currentPosition - 60 > 0)
  456. {
  457. player.Ctlcontrols.currentPosition -= 60;
  458. }
  459. else if (player.currentMedia != null && (int)player.Ctlcontrols.currentPosition <= 60)
  460. {
  461. player.Ctlcontrols.currentPosition = 0;
  462. }
  463. }
  464.  
  465.  
  466. //Scroll Up
  467. private void button12_Click(object sender, EventArgs e)
  468. {
  469. int songCount = GetSongCount();
  470. if(buttons[0].Location.Y == 13)
  471. {
  472. return;
  473. }
  474. if (songCount > 9)
  475. {
  476. foreach (Button b in buttons)
  477. {
  478. if (b != null)
  479. {
  480. b.Location = new Point(b.Location.X, b.Location.Y + 15);
  481. }
  482. }
  483.  
  484. }
  485. ButtonVisibility();
  486. }
  487.  
  488. //Scroll Down
  489. private void button13_Click(object sender, EventArgs e)
  490. {
  491. int lastSongIndex = -1;
  492. for (int i = 0; i < buttons.Length; i++)
  493. {
  494. if (buttons[i] == null)
  495. {
  496. lastSongIndex = i - 1;
  497. break;
  498. }
  499. }
  500. //13 + 15 * 8 = 133, if the last song reaches that then dont go any further.
  501. if(buttons[lastSongIndex].Location.Y == 133)
  502. {
  503. return;
  504. }
  505.  
  506.  
  507. int songCount = GetSongCount();
  508.  
  509. if (songCount > 9)
  510. {
  511. foreach (Button b in buttons)
  512. {
  513. if (b != null)
  514. {
  515. b.Location = new Point(b.Location.X, b.Location.Y - 15);
  516.  
  517. }
  518. }
  519.  
  520. }
  521.  
  522. ButtonVisibility();
  523.  
  524. }
  525.  
  526. //Download Button
  527. private async void button14_Click(object sender, EventArgs e)
  528. {
  529. downloadClicked++;
  530.  
  531. if(downloadClicked == 3)
  532. {
  533. label3.Visible = false;
  534. txtUrl.Visible = false;
  535. downloadClicked = 0;
  536. return;
  537. }
  538.  
  539. if(downloadClicked == 1)
  540. {
  541. label3.Visible = true;
  542. txtUrl.Visible = true;
  543. return;
  544. }
  545.  
  546. using (FolderBrowserDialog fbd = new FolderBrowserDialog())
  547. {
  548. label3.Text = "STATUS: SELECTING FOLDER";
  549. fbd.ShowDialog();
  550. if (fbd.SelectedPath == null)
  551. {
  552. return;
  553. }
  554. try
  555. {
  556. label3.Text = "STATUS: DOWNLOADING";
  557. var youtube = YouTube.Default;
  558. var video = await youtube.GetVideoAsync(txtUrl.Text);
  559. string path = fbd.SelectedPath + @"\" + video.FullName;
  560. File.WriteAllBytes(path, await video.GetBytesAsync());
  561.  
  562.  
  563. Song s = new Song(fbd.SelectedPath + @"\" + video.FullName);
  564. //save the songs address to the list
  565. using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"Songs.txt", true))
  566. {
  567. file.WriteLine(s.directory);
  568. }
  569.  
  570. Reload();
  571. label3.Text = "STATUS: SUCCESS";
  572. } catch(Exception ee)
  573. {
  574. label3.Text = "STATUS: ";
  575. MessageBox.Show(ee.Message);
  576. }
  577.  
  578.  
  579. }
  580.  
  581.  
  582.  
  583.  
  584.  
  585.  
  586. }
  587.  
  588. #endregion
  589.  
  590. #region Custom Functions
  591. //Duration to look better on screen
  592. string DurationToMin()
  593. {
  594. int minutes = 0;
  595. int seconds = 0;
  596. int duration = (int)player.currentMedia.duration;
  597. string toReturn;
  598. if(player.currentMedia != null)
  599. {
  600. if(duration <= 59 && duration >= 0)
  601. {
  602. if(duration <= 9)
  603. {
  604. toReturn = "00:0" + duration;
  605. return toReturn;
  606. } else
  607. {
  608. toReturn = "00:" + duration;
  609. return toReturn;
  610. }
  611.  
  612. } else
  613. {
  614. do
  615. {
  616. duration -= 60;
  617. minutes++;
  618. } while (duration - 60 >= 60);
  619. duration = (int)player.currentMedia.duration;
  620. seconds = duration - (minutes * 60);
  621.  
  622. if (seconds >= 60)
  623. {
  624. minutes++;
  625. seconds = duration - (minutes * 60);
  626. }
  627.  
  628.  
  629. if (minutes <= 9)
  630. {
  631. if (seconds <= 9)
  632. {
  633. toReturn = "0" + minutes + ":0" + seconds;
  634. return toReturn;
  635. }
  636. else
  637. {
  638. toReturn = "0" + minutes + ":" + seconds;
  639. return toReturn;
  640. }
  641.  
  642. }
  643. else
  644. {
  645.  
  646. if (seconds <= 9)
  647. {
  648. toReturn = minutes + ":0" + seconds;
  649. return toReturn;
  650. }
  651. else
  652. {
  653. toReturn = minutes + ":" + seconds;
  654. return toReturn;
  655. }
  656.  
  657. }
  658.  
  659.  
  660. }
  661.  
  662.  
  663.  
  664.  
  665.  
  666. }
  667.  
  668.  
  669.  
  670. return "";
  671. }
  672. //Make seconds look better on screen
  673. string SecToMin()
  674. {
  675. int minutes = 0;
  676. int seconds = 0;
  677. string toReturn;
  678. if (player.currentMedia != null)
  679. {
  680. int pos = (int)player.Ctlcontrols.currentPosition;
  681. //Under a Minute
  682. if(pos <= 59 && pos >= 0)
  683. {
  684. if(pos <= 9)
  685. {
  686. toReturn = "00:0" + pos;
  687. return toReturn;
  688. }else
  689. {
  690. toReturn = "00:" + pos;
  691. return toReturn;
  692. }
  693.  
  694.  
  695. }
  696.  
  697. else
  698.  
  699. {
  700. do
  701. {
  702. pos -= 60;
  703. minutes++;
  704. } while (pos - 60 >= 60);
  705.  
  706. pos = (int)player.Ctlcontrols.currentPosition;
  707. seconds = pos - (minutes * 60);
  708. if(seconds >= 60)
  709. {
  710. minutes++;
  711. seconds = pos - (minutes * 60);
  712. }
  713. Console.WriteLine(minutes + " " +seconds);
  714.  
  715. if (minutes <= 9)
  716. {
  717. if (seconds <= 9)
  718. {
  719. toReturn = "0" + minutes+ ":0" + seconds;
  720. return toReturn;
  721. }
  722. else
  723. {
  724. toReturn = "0" + minutes + ":" + seconds;
  725. return toReturn;
  726. }
  727.  
  728. } else
  729. {
  730.  
  731. if (seconds <= 9)
  732. {
  733. toReturn = minutes + ":0" + seconds;
  734. return toReturn;
  735. }
  736. else
  737. {
  738. toReturn = minutes + ":" + seconds;
  739. return toReturn;
  740. }
  741.  
  742. }
  743.  
  744.  
  745.  
  746.  
  747.  
  748.  
  749.  
  750. }
  751.  
  752.  
  753. }
  754. return "";
  755. }
  756. //Remove all song buttons
  757. void RemoveButtons()
  758. {
  759. //Clear Controls
  760. foreach (Button b in buttons)
  761. {
  762. Controls.Remove(b);
  763. }
  764. }
  765. //Read a file in normally
  766. void ReadFile()
  767. {
  768. index = 0;
  769. //ReRead the text file and create new array
  770. //create text file if it doesnt exist, if it does load all the songs in.
  771. if (System.IO.File.Exists(@"Songs.txt"))
  772. {
  773.  
  774. string[] lines = System.IO.File.ReadAllLines(@"Songs.txt");
  775.  
  776. foreach (string line in lines)
  777. {
  778. Song song = new Song(line);
  779. songs[index] = song;
  780. index++;
  781. }
  782. }
  783. else
  784. {
  785. System.IO.File.Create(@"Songs.txt");
  786. }
  787. }
  788.  
  789. //Write a new file without a specific song
  790. void ReWriteFile(int songToRemove)
  791. {
  792. //Create a new writer
  793. using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"Songs.txt", false))
  794. {
  795. //New song array to put in text file
  796. string[] dirs = new string[128];
  797.  
  798. //Go through all songs
  799. for (int i = 0; i < songs.Length; i++)
  800. {
  801. //If the song exists and it is not the song to remove, if it is then skip it.
  802. if (songs[i] != null && i != songToRemove)
  803. {
  804. //Write it to the new file
  805. dirs[i] = songs[i].directory;
  806. file.WriteLine(dirs[i]);
  807. }
  808. }
  809.  
  810. }
  811. }
  812.  
  813. //Check Duplicate
  814. bool CheckDuplicate(Song song)
  815. {
  816. foreach (Song s in songs)
  817. {
  818. if (s != null)
  819. {
  820. if (s.name == song.name)
  821. {
  822. DialogResult dialogResult = MessageBox.Show("The song: " + song.name + " is already added. Do you still want to add it?", "Duplicate Found", MessageBoxButtons.YesNo);
  823. if (dialogResult == DialogResult.Yes)
  824. {
  825. return false;
  826. }
  827.  
  828. if (dialogResult == DialogResult.No)
  829. {
  830. return true;
  831. }
  832. }
  833. }
  834.  
  835. }
  836. return false;
  837. }
  838.  
  839. //Create list of songs on screen
  840. void CreateSongListOS()
  841. {
  842.  
  843. //Create list of songs on screen
  844. int songIndex = 0;
  845. foreach (Song a in songs)
  846. {
  847. if (a != null)
  848. {
  849. Button b = new Button();
  850. #region Button Config
  851. b.AutoSize = false;
  852. b.Font = new Font("Microsoft Sans Serif", 8);
  853. b.UseMnemonic = false;
  854. b.BackColor = Color.Transparent;
  855. b.ForeColor = Color.Transparent;
  856. b.Location = new Point(nextLabelX, nextLabelY);
  857. b.Size = new Size(385, 20);
  858. b.FlatStyle = FlatStyle.Flat;
  859. b.FlatAppearance.BorderSize = 0;
  860. b.TextAlign = ContentAlignment.MiddleLeft;
  861. b.Text = a.name;
  862. nextLabelY += 15;
  863. button1.Name = "Button";
  864. b.Click += new EventHandler(songButton_Click);
  865. #endregion
  866.  
  867. Controls.Add(b);
  868. buttons[songIndex] = b;
  869. songIndex++;
  870.  
  871. }
  872.  
  873. }
  874. }
  875.  
  876. //Remove Song
  877. void RemoveSong(int songToRemove)
  878. {
  879. //Create a new writer
  880. using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"Songs.txt", false))
  881. {
  882. //New song array to put in text file
  883. string[] dirs = new string[128];
  884.  
  885. //Go through all songs
  886. for (int i = 0; i < songs.Length; i++)
  887. {
  888. //If the song exists and it is not the song to remove, if it is then skip it.
  889. if (songs[i] != null && i != songToRemove)
  890. {
  891. //Write it to the new file
  892. dirs[i] = songs[i].directory;
  893. file.WriteLine(dirs[i]);
  894. }
  895. }
  896.  
  897. }
  898. }
  899.  
  900. //If button is outside bounds make it invisible
  901. void ButtonVisibility()
  902. {
  903. //From that point the songs will be invisible
  904. int buttonRange = 140;
  905. foreach (Button b in buttons)
  906. {
  907. if (b != null)
  908. {
  909. int y = b.Location.Y;
  910. if (y > buttonRange)
  911. {
  912. b.Visible = false;
  913. }
  914. }
  915.  
  916. }
  917. foreach (Button b in buttons)
  918. {
  919. if (b != null)
  920. {
  921. int y = b.Location.Y;
  922. if (y < buttonRange)
  923. {
  924. b.Visible = true;
  925. }
  926. }
  927.  
  928. }
  929. }
  930.  
  931. //Reload all songs from file
  932. void Reload()
  933. {
  934. selectedIndex = 0;
  935. nextLabelY = 13;
  936.  
  937.  
  938. //Reset songs array
  939. for (int i = 0; i < songs.Length; i++)
  940. {
  941. if (songs[i] != null)
  942. {
  943. songs[i] = null;
  944.  
  945. }
  946. }
  947.  
  948. ReadFile();
  949.  
  950. RemoveButtons();
  951. CreateSongListOS();
  952.  
  953. ButtonVisibility();
  954. player.Ctlcontrols.stop();
  955. }
  956.  
  957. //Get the song Count in songs array
  958. int GetSongCount()
  959. {
  960. int songCount = 0;
  961. foreach (Song s in songs)
  962. {
  963. if (s != null)
  964. {
  965. songCount++;
  966. }
  967. }
  968. return songCount;
  969.  
  970. }
  971.  
  972. //Highlight a song
  973. void HighLightText(int songToHighlight)
  974. {
  975.  
  976. int index = 0;
  977.  
  978. foreach (Button b in buttons)
  979. {
  980. if (b != null)
  981. {
  982. if (index == songToHighlight)
  983. {
  984. b.ForeColor = Color.Red;
  985.  
  986. }
  987. else
  988. {
  989. b.ForeColor = Color.White;
  990. }
  991. }
  992.  
  993.  
  994. index++;
  995. }
  996. }
  997.  
  998. #endregion
  999.  
  1000. #region Timers
  1001. //Update progress bar
  1002. private void timer1_Tick(object sender, EventArgs e)
  1003. {
  1004.  
  1005. if (player.currentMedia != null)
  1006. {
  1007. //Set max size for progress bar
  1008. progressBar1.Maximum = (int)player.currentMedia.duration;
  1009. //Current pos on progress
  1010. progressBar1.Value = (int)player.Ctlcontrols.currentPosition;
  1011. //Update progressbar
  1012. progressBar1.Update();
  1013.  
  1014. //If the song is over stop the timer.
  1015. if ((int)player.currentMedia.duration >= (int)player.Ctlcontrols.currentPosition)
  1016. {
  1017.  
  1018. timer1.Stop();
  1019. }
  1020. }
  1021.  
  1022. }
  1023.  
  1024. //This updates song stats, current time(s) and duration(s)
  1025. private void timer2_Tick(object sender, EventArgs e)
  1026. {
  1027.  
  1028. if (player.currentMedia != null)
  1029. {
  1030. //make them int so it looks nice
  1031. int currentSec = (int)player.Ctlcontrols.currentPosition;
  1032. int duration = (int)player.currentMedia.duration;
  1033.  
  1034.  
  1035.  
  1036. //Update text
  1037. label1.Text = DurationToMin();
  1038. label2.Text = SecToMin();
  1039. }
  1040.  
  1041. }
  1042.  
  1043. //Delay progress bar launch
  1044. private void timer3_Tick(object sender, EventArgs e)
  1045. {
  1046. timer1.Start();
  1047. }
  1048.  
  1049. //Shuffle Timer
  1050. private void timer4_Tick(object sender, EventArgs e)
  1051. {
  1052.  
  1053. if (shuffleActivated)
  1054. {
  1055.  
  1056. if ((int)player.Ctlcontrols.currentPosition == (int)player.currentMedia.duration - 1)
  1057. {
  1058. shuffle.Next(ref player);
  1059. player.URL = songs[shuffle.currentSong].directory;
  1060. player.Ctlcontrols.play();
  1061. HighLightText(shuffle.currentSong);
  1062. }
  1063.  
  1064. }
  1065.  
  1066. }
  1067. //AutoPlay Timer
  1068. private void timer7_Tick(object sender, EventArgs e)
  1069. {
  1070. if (!shuffleActivated)
  1071. {
  1072. if(player.currentMedia != null)
  1073. {
  1074. if ((int)player.Ctlcontrols.currentPosition == (int)player.currentMedia.duration - 1)
  1075. {
  1076. if (songs[selectedIndex + 1] != null)
  1077. {
  1078. selectedIndex += 1;
  1079. player.URL = songs[selectedIndex].directory;
  1080. HighLightText(selectedIndex);
  1081. player.Ctlcontrols.play();
  1082. } else
  1083. {
  1084. player.Ctlcontrols.stop();
  1085. }
  1086.  
  1087.  
  1088. }
  1089. }
  1090.  
  1091. }
  1092.  
  1093. }
  1094. #endregion
  1095.  
  1096. #region Volume
  1097. //TRACKBAR - Volume slider.
  1098. private void trackBar1_Scroll(object sender, EventArgs e)
  1099. {
  1100. player.settings.volume = trackBar1.Value;
  1101. }
  1102. #endregion
  1103.  
  1104. #region Classes
  1105. //Shuffle Class
  1106. public class Shuffle
  1107. {
  1108. #region Variables
  1109. public int currentSong;
  1110. int[] songList;
  1111. int songCount;
  1112. Random random;
  1113. public int index;
  1114. #endregion
  1115.  
  1116. #region Functions
  1117. public void Next(ref AxWMPLib.AxWindowsMediaPlayer player)
  1118. {
  1119.  
  1120. if (index == songList.Length - 1)
  1121. {
  1122. player.Ctlcontrols.stop();
  1123. return;
  1124. }
  1125. if (index < songList.Length)
  1126. {
  1127. currentSong = songList[++index];
  1128. }
  1129.  
  1130. }
  1131.  
  1132. void reshuffle(int[] indexes)
  1133. {
  1134.  
  1135. // Knuth shuffle algorithm :: courtesy of Wikipedia :)
  1136. for (int t = 0; t < indexes.Length; t++)
  1137. {
  1138. int tmp = indexes[t];
  1139. int r = random.Next(t, indexes.Length - 1);
  1140. indexes[t] = indexes[r];
  1141. indexes[r] = tmp;
  1142. }
  1143. }
  1144. #endregion
  1145.  
  1146. #region Constructor
  1147. public Shuffle(ref Song[] songs)
  1148. {
  1149. index = 0;
  1150. random = new Random();
  1151.  
  1152. //Get song count
  1153. for (int i = 0; i < songs.Length; i++)
  1154. {
  1155. if (songs[i] != null)
  1156. {
  1157. songCount++;
  1158. }
  1159. }
  1160.  
  1161. songList = new int[songCount];
  1162.  
  1163. for (int i = 0; i < songCount; i++)
  1164. {
  1165. songList[i] = i;
  1166. }
  1167. //Rearrange songList Array
  1168. reshuffle(songList);
  1169. //Set current song
  1170. currentSong = songList[index];
  1171.  
  1172. Console.WriteLine("ORDER OF SHUFFLE: ");
  1173. for (int i = 0; i < songList.Length; i++)
  1174. {
  1175. Console.WriteLine(songList[i]);
  1176. }
  1177. }
  1178.  
  1179. #endregion
  1180. }
  1181.  
  1182. //Song Class
  1183. public class Song
  1184. {
  1185. #region Variables
  1186. public string name;
  1187. public string directory;
  1188. public bool success = false;
  1189. public bool playing = false;
  1190. #endregion
  1191. #region Constructors
  1192. public Song()
  1193. {
  1194. //For opening a file
  1195. OpenFileDialog openFile = new OpenFileDialog();
  1196.  
  1197. //Show windows explorer and open file
  1198. if (openFile.ShowDialog() == DialogResult.OK)
  1199. {
  1200. //Init variables
  1201. directory = openFile.FileName;
  1202. name = openFile.SafeFileName;
  1203. //save the songs address to the list
  1204. using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"Songs.txt", true))
  1205. {
  1206. file.WriteLine(directory);
  1207. }
  1208. success = true;
  1209. }
  1210. else
  1211. {
  1212. success = false;
  1213. }
  1214. }
  1215.  
  1216. //Alternative constructor for a song. This is for loading in a song from the text file.
  1217. public Song(string directory)
  1218. {
  1219. OpenFileDialog openFile = new OpenFileDialog();
  1220. openFile.FileName = directory;
  1221. this.name = openFile.SafeFileName;
  1222. this.directory = directory;
  1223. }
  1224. #endregion
  1225. }
  1226. #endregion
  1227.  
  1228. #region Random Unused
  1229. //Duration of the song
  1230. private void label1_Click(object sender, EventArgs e)
  1231. {
  1232. }
  1233.  
  1234. //currentSec
  1235. private void label2_Click(object sender, EventArgs e)
  1236. {
  1237. }
  1238.  
  1239. //Progress bar showing how far the song is
  1240. public void progressBar1_Click(object sender, EventArgs e)
  1241. {
  1242.  
  1243. }
  1244.  
  1245. //Necessary >D
  1246. private void Form1_Load(object sender, EventArgs e)
  1247. {
  1248. label3.Visible = false;
  1249. txtUrl.Visible = false;
  1250. timer7.Start();
  1251.  
  1252. }
  1253.  
  1254. private void txtUrl_TextChanged(object sender, EventArgs e)
  1255. {
  1256.  
  1257. }
  1258.  
  1259. private void label3_Click(object sender, EventArgs e)
  1260. {
  1261.  
  1262. }
  1263.  
  1264.  
  1265. #endregion
  1266.  
  1267.  
  1268. }
  1269.  
  1270. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement