Advertisement
Guest User

Untitled

a guest
May 30th, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.63 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.IO;
  11. using System.Media;
  12. using WMPLib;
  13. using System.Runtime.InteropServices;
  14. using System.Diagnostics;
  15.  
  16. namespace RohuPlayer
  17. {
  18.  
  19.  
  20.  
  21. public partial class Form1 : Form
  22. {
  23.  
  24.  
  25.  
  26. //true if user has selected new song
  27. bool changeWanted = false;
  28. //If previous was pressed twice;
  29.  
  30. int doublePrevClicks = 0;
  31.  
  32. //shuffle
  33. bool shuffleActivated = false;
  34. //MEDIA PLAYER
  35. AxWMPLib.AxWindowsMediaPlayer player;
  36.  
  37. //Current Song that is playing
  38. public int playing;
  39.  
  40. //Song array cointaining all the songs in the program
  41. public Song[] songs;
  42.  
  43. //index for songs
  44. public int index = 0;
  45.  
  46. //Last index of a song played-Used to not replay the song if user clicks on white space in linebox.
  47. private int lastIndex = -1;
  48.  
  49. Shuffle shuffle;
  50. //Constructor for program
  51. public Form1()
  52. {
  53. InitializeComponent();
  54. //Create song array
  55. songs = new Song[128];
  56. //Initialize media player
  57. player = new AxWMPLib.AxWindowsMediaPlayer();
  58. //Create controls for media player
  59. player.CreateControl();
  60. //Set the current volume to the programs volume bar
  61. trackBar1.Value = player.settings.volume;
  62.  
  63. //create text file if it doesnt exist, if it does load all the songs in.
  64. if (System.IO.File.Exists(@"Songs.txt")){
  65.  
  66. string[] lines = System.IO.File.ReadAllLines(@"Songs.txt");
  67.  
  68. foreach (string line in lines)
  69. {
  70. Song song = new Song(line);
  71. songs[index] = song;
  72. index++;
  73. }
  74. } else
  75. {
  76. System.IO.File.Create(@"Songs.txt");
  77. }
  78.  
  79.  
  80. //Create list of songs on screen
  81. foreach (Song a in songs)
  82. {
  83. if(a != null)
  84. listBox1.Items.Add(a.name);
  85. }
  86. Console.WriteLine(listBox1.Size.Width);
  87. Console.WriteLine(listBox1.Size.Height);
  88. }
  89.  
  90.  
  91. //BUTTON 1 - Add a song
  92. private void button1_Click(object sender, EventArgs e)
  93. {
  94. Song song = new Song();
  95. if (song.success)
  96. {
  97. songs[index] = song;
  98. index++;
  99. Reload();
  100. }
  101.  
  102.  
  103. }
  104.  
  105. //BUTTON 6 - Remove a song
  106. private void button6_Click(object sender, EventArgs e)
  107. {
  108. //Song index to remove
  109. int songToRemove = listBox1.SelectedIndex;
  110.  
  111. //Double Check
  112. DialogResult dialogResult = MessageBox.Show("Do you want to remove the song: " + songs[songToRemove].name, "Remove Song?", MessageBoxButtons.YesNo);
  113.  
  114. if(dialogResult == DialogResult.Yes)
  115. {
  116.  
  117. //If the song to remove is an actual song
  118. if (songToRemove != -1)
  119. {
  120. //Remove it from the list
  121. listBox1.Items.RemoveAt(songToRemove);
  122.  
  123. //Create a new writer
  124. using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"Songs.txt", false))
  125. {
  126. //New song array to put in text file
  127. string[] dirs = new string[128];
  128.  
  129. //Go through all songs
  130. for (int i = 0; i < songs.Length; i++)
  131. {
  132. //If the song exists and it is not the song to remove, if it is then skip it.
  133. if (songs[i] != null && i != songToRemove)
  134. {
  135. //Write it to the new file
  136. dirs[i] = songs[i].directory;
  137. file.WriteLine(dirs[i]);
  138. }
  139. }
  140.  
  141. }
  142. }
  143.  
  144. }
  145.  
  146. //Reload songs and stop player
  147. if (dialogResult == DialogResult.Yes)
  148. {
  149. player.Ctlcontrols.stop();
  150. Reload();
  151. }
  152.  
  153.  
  154.  
  155. }
  156.  
  157.  
  158.  
  159. //BUTTON 2 - Play Button
  160. private void button2_Click(object sender, EventArgs e)
  161. {
  162.  
  163.  
  164.  
  165. if(listBox1.SelectedIndex <= songs.Length && listBox1.SelectedIndex >= 0 && songs[listBox1.SelectedIndex] != null && changeWanted)
  166. {
  167. //Set new song up
  168. player.URL = songs[listBox1.SelectedIndex].directory;
  169. playing = listBox1.SelectedIndex;
  170. //Stop it from autoplaying
  171. // player.Ctlcontrols.stop();
  172.  
  173. //Play
  174. player.Ctlcontrols.play();
  175.  
  176. //Start the current second timer.
  177. timer2.Start();
  178. //Delay timer1 for 2 sec, because it does not work otherwise.
  179. timer3.Start();
  180. changeWanted = false;
  181. } else
  182. {
  183. player.Ctlcontrols.play();
  184. }
  185.  
  186.  
  187.  
  188. }
  189.  
  190.  
  191.  
  192.  
  193. //BUTTON 4 - Pause button
  194. private void button4_Click(object sender, EventArgs e)
  195. {
  196. player.Ctlcontrols.pause();
  197. }
  198.  
  199. //BUTTON 5 - Skip ten seconds of the song
  200. private void button5_Click(object sender, EventArgs e)
  201. {
  202. if (player.currentMedia != null && player.Ctlcontrols.currentPosition + 10 < player.currentMedia.duration)
  203. {
  204. player.Ctlcontrols.currentPosition += 10;
  205. }
  206. }
  207.  
  208. //BUTTON 3 - Go back ten seconds
  209. private void button3_Click(object sender, EventArgs e)
  210. {
  211. if (player.currentMedia != null && player.Ctlcontrols.currentPosition - 10 > 10)
  212. {
  213. player.Ctlcontrols.currentPosition -= 10;
  214. } else if (player.currentMedia != null && (int)player.Ctlcontrols.currentPosition <= 10)
  215. {
  216. player.Ctlcontrols.currentPosition = 0;
  217. }
  218. }
  219.  
  220. //BUTTON 7 - Shuffle
  221. private void button7_Click(object sender, EventArgs e)
  222. {
  223.  
  224. shuffleActivated = !shuffleActivated;
  225. shuffle = new Shuffle(ref songs);
  226. if (shuffleActivated)
  227. {
  228. player.Ctlcontrols.stop();
  229.  
  230. player.URL = songs[shuffle.currentSong].directory;
  231. listBox1.SetSelected(shuffle.currentSong, true);
  232.  
  233. //Start the current second timer.
  234. timer2.Start();
  235. //Delay timer1 for 2 sec, because it does not work otherwise.
  236. timer3.Start();
  237. timer4.Start();
  238. }
  239.  
  240.  
  241.  
  242. }
  243.  
  244.  
  245. //BUTTON 8 - SKIP
  246. private void button8_Click(object sender, EventArgs e)
  247. {
  248.  
  249. playing++;
  250.  
  251. if (shuffleActivated)
  252. {
  253. shuffle.Next(ref player);
  254. player.URL = songs[shuffle.currentSong].directory;
  255. listBox1.SetSelected(shuffle.currentSong, true);
  256. player.Ctlcontrols.play();
  257. return;
  258. }
  259.  
  260. if(songs[playing] == null)
  261. {
  262. playing--;
  263. } else
  264. {
  265. player.URL = songs[playing].directory;
  266. player.Ctlcontrols.play();
  267. listBox1.SetSelected(playing, true);
  268. }
  269.  
  270. }
  271.  
  272. //BUTTON 9 - PREVIOUS
  273. private void button9_Click(object sender, EventArgs e)
  274. {
  275. doublePrevClicks++;
  276. if(doublePrevClicks >= 2)
  277. {
  278. playing--;
  279. if(playing < 0)
  280. {
  281. playing = 0;
  282. } else
  283. {
  284. player.URL = songs[playing].directory;
  285. player.Ctlcontrols.play();
  286. listBox1.SetSelected(playing, true);
  287. doublePrevClicks = 0;
  288. }
  289.  
  290. }
  291. else
  292. {
  293. player.Ctlcontrols.stop();
  294. player.Ctlcontrols.play();
  295. }
  296.  
  297.  
  298. }
  299.  
  300.  
  301. //BUTTON 10 - +60 seconds
  302. private void button10_Click(object sender, EventArgs e)
  303. {
  304. if (player.currentMedia != null && player.Ctlcontrols.currentPosition + 60 < player.currentMedia.duration)
  305. {
  306. player.Ctlcontrols.currentPosition += 60;
  307. }
  308. }
  309.  
  310. private void button11_Click(object sender, EventArgs e)
  311. {
  312. if (player.currentMedia != null && player.Ctlcontrols.currentPosition - 60 > 0)
  313. {
  314. player.Ctlcontrols.currentPosition -= 60;
  315. }
  316. else if (player.currentMedia != null && (int)player.Ctlcontrols.currentPosition <= 60)
  317. {
  318. player.Ctlcontrols.currentPosition = 0;
  319. }
  320. }
  321.  
  322.  
  323.  
  324. //LISTBOX -- If a new song was selected
  325. private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
  326. {
  327. //If its not -1 then the click was on an actual song
  328. if (listBox1.SelectedIndex != -1 && listBox1.SelectedIndex != lastIndex)
  329. {
  330. //Set last index to stop it from playing again when clicked
  331. lastIndex = listBox1.SelectedIndex;
  332. playing = listBox1.SelectedIndex;
  333. changeWanted = true;
  334. }
  335.  
  336. }
  337.  
  338.  
  339.  
  340. //TRACKBAR - Volume slider.
  341. private void trackBar1_Scroll(object sender, EventArgs e)
  342. {
  343. player.settings.volume = trackBar1.Value;
  344.  
  345. }
  346.  
  347.  
  348.  
  349.  
  350.  
  351.  
  352.  
  353.  
  354. //TIMER - Update progress bar
  355. private void timer1_Tick(object sender, EventArgs e)
  356. {
  357.  
  358. if (player.currentMedia != null)
  359. {
  360. //Set max size for progress bar
  361. progressBar1.Maximum = (int)player.currentMedia.duration;
  362. //Current pos on progress
  363. progressBar1.Value = (int)player.Ctlcontrols.currentPosition;
  364. //Update progressbar
  365. progressBar1.Update();
  366.  
  367. //If the song is over stop the timer.
  368. if ((int)player.currentMedia.duration >= (int)player.Ctlcontrols.currentPosition)
  369. {
  370.  
  371. timer1.Stop();
  372. }
  373. }
  374.  
  375. }
  376.  
  377. //TIMER - This updates song stats, current time(s) and duration(s)
  378. private void timer2_Tick(object sender, EventArgs e)
  379. {
  380.  
  381. if (player.currentMedia != null)
  382. {
  383. //make them int so it looks nice
  384. int currentSec = (int)player.Ctlcontrols.currentPosition;
  385. int duration = (int)player.currentMedia.duration;
  386.  
  387. //Update text
  388. label1.Text = duration.ToString() + " s";
  389. label2.Text = currentSec.ToString() + " s";
  390. }
  391.  
  392. }
  393.  
  394. //TIMER - Delay progress bar launch
  395. private void timer3_Tick(object sender, EventArgs e)
  396. {
  397. timer1.Start();
  398. }
  399.  
  400.  
  401. //SHUFFLE TIMER
  402. private void timer4_Tick(object sender, EventArgs e)
  403. {
  404.  
  405. if (shuffleActivated)
  406. {
  407.  
  408. if ((int)player.Ctlcontrols.currentPosition == (int)player.currentMedia.duration - 1)
  409. {
  410. shuffle.Next(ref player);
  411. player.URL = songs[shuffle.currentSong].directory;
  412. listBox1.SetSelected(shuffle.currentSong, true);
  413. player.Ctlcontrols.play();
  414. }
  415.  
  416. }
  417. }
  418.  
  419.  
  420. //Reload all songs from file
  421. void Reload()
  422. {
  423. index = 0;
  424. lastIndex = -1;
  425. //Reset songs array
  426. for(int i = 0; i < songs.Length; i++)
  427. {
  428. if(songs[i] != null)
  429. {
  430. songs[i] = null;
  431.  
  432. }
  433. }
  434.  
  435. //ReRead the text file and create new array
  436. //create text file if it doesnt exist, if it does load all the songs in.
  437. if (System.IO.File.Exists(@"Songs.txt"))
  438. {
  439.  
  440. string[] lines = System.IO.File.ReadAllLines(@"Songs.txt");
  441.  
  442. foreach (string line in lines)
  443. {
  444. Song song = new Song(line);
  445. songs[index] = song;
  446. index++;
  447. }
  448. }
  449. else
  450. {
  451. System.IO.File.Create(@"Songs.txt");
  452. }
  453.  
  454. //Clear listbox
  455. listBox1.Items.Clear();
  456. //Create list of songs on screen
  457. foreach (Song a in songs)
  458. {
  459. if (a != null)
  460. listBox1.Items.Add(a.name);
  461. }
  462.  
  463. }
  464.  
  465.  
  466.  
  467.  
  468. //Shuffle Class
  469. public class Shuffle
  470. {
  471.  
  472. public int currentSong;
  473. int[] songList;
  474. int songCount;
  475. Random random;
  476.  
  477. int index;
  478.  
  479. public void Next(ref AxWMPLib.AxWindowsMediaPlayer player)
  480. {
  481. if(index == songList.Length - 1)
  482. {
  483. player.Ctlcontrols.stop();
  484. return;
  485. }
  486. if (index < songList.Length)
  487. {
  488. currentSong = songList[++index];
  489. }
  490.  
  491. }
  492.  
  493. void reshuffle(int[] indexes)
  494. {
  495.  
  496. // Knuth shuffle algorithm :: courtesy of Wikipedia :)
  497. for (int t = 0; t < indexes.Length; t++)
  498. {
  499. int tmp = indexes[t];
  500. int r = random.Next(t, indexes.Length - 1);
  501. indexes[t] = indexes[r];
  502. indexes[r] = tmp;
  503. }
  504. }
  505.  
  506. public Shuffle(ref Song[] songs)
  507. {
  508.  
  509. index = 0;
  510.  
  511.  
  512. random = new Random();
  513.  
  514. //Get song count
  515. for (int i = 0; i < songs.Length; i++)
  516. {
  517. if(songs[i] != null)
  518. {
  519. songCount++;
  520. }
  521. }
  522. songList = new int[songCount];
  523.  
  524. for (int i = 0; i < songCount; i++)
  525. {
  526. songList[i] = i;
  527. }
  528.  
  529. reshuffle(songList);
  530.  
  531.  
  532. currentSong = songList[index];
  533.  
  534. Console.WriteLine("ORDER OF SHUFFLE: ");
  535. for(int i = 0; i < songList.Length; i++)
  536. {
  537. Console.WriteLine(songList[i]);
  538. }
  539. }
  540.  
  541.  
  542. }
  543.  
  544. //Song Class
  545. public class Song
  546. {
  547. //Variables for a song
  548. public string name;
  549. public string directory;
  550. public bool success = false;
  551. public bool playing = false;
  552.  
  553.  
  554. public Song()
  555. {
  556. //For opening a file
  557. OpenFileDialog openFile = new OpenFileDialog();
  558.  
  559. //Show windows explorer and open file
  560. if (openFile.ShowDialog() == DialogResult.OK)
  561. {
  562. //Init variables
  563. directory = openFile.FileName;
  564. name = openFile.SafeFileName;
  565. //save the songs address to the list
  566. using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"Songs.txt", true))
  567. {
  568. file.WriteLine(directory);
  569. }
  570.  
  571. success = true;
  572.  
  573. }
  574. else
  575. {
  576. success = false;
  577. }
  578.  
  579. }
  580. //Alternative constructor for a song. This is for loading in a song from the text file.
  581. public Song(string directory)
  582. {
  583. OpenFileDialog openFile = new OpenFileDialog();
  584. openFile.FileName = directory;
  585. this.name = openFile.SafeFileName;
  586.  
  587. this.directory = directory;
  588.  
  589. }
  590.  
  591. }
  592.  
  593.  
  594.  
  595.  
  596. //UNUSED STUFF
  597.  
  598. //Duration of the song
  599. private void label1_Click(object sender, EventArgs e)
  600. {
  601. }
  602.  
  603.  
  604.  
  605. //currentSec
  606. private void label2_Click(object sender, EventArgs e)
  607. {
  608. }
  609.  
  610. //Progress bar showing how far the song is
  611. public void progressBar1_Click(object sender, EventArgs e)
  612. {
  613.  
  614. }
  615.  
  616.  
  617.  
  618. //Unnecessary
  619. private void Form1_Load(object sender, EventArgs e)
  620. {
  621.  
  622. }
  623.  
  624.  
  625. }
  626.  
  627.  
  628. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement