Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.IO;
- using System.Media;
- using WMPLib;
- using System.Runtime.InteropServices;
- using System.Diagnostics;
- namespace RohuPlayer
- {
- public partial class Form1 : Form
- {
- //true if user has selected new song
- bool changeWanted = false;
- //If previous was pressed twice;
- int doublePrevClicks = 0;
- //shuffle
- bool shuffleActivated = false;
- //MEDIA PLAYER
- AxWMPLib.AxWindowsMediaPlayer player;
- //Current Song that is playing
- public int playing;
- //Song array cointaining all the songs in the program
- public Song[] songs;
- //index for songs
- public int index = 0;
- //Last index of a song played-Used to not replay the song if user clicks on white space in linebox.
- private int lastIndex = -1;
- Shuffle shuffle;
- //Constructor for program
- public Form1()
- {
- InitializeComponent();
- //Create song array
- songs = new Song[128];
- //Initialize media player
- player = new AxWMPLib.AxWindowsMediaPlayer();
- //Create controls for media player
- player.CreateControl();
- //Set the current volume to the programs volume bar
- trackBar1.Value = player.settings.volume;
- //create text file if it doesnt exist, if it does load all the songs in.
- if (System.IO.File.Exists(@"Songs.txt")){
- string[] lines = System.IO.File.ReadAllLines(@"Songs.txt");
- foreach (string line in lines)
- {
- Song song = new Song(line);
- songs[index] = song;
- index++;
- }
- } else
- {
- System.IO.File.Create(@"Songs.txt");
- }
- //Create list of songs on screen
- foreach (Song a in songs)
- {
- if(a != null)
- listBox1.Items.Add(a.name);
- }
- Console.WriteLine(listBox1.Size.Width);
- Console.WriteLine(listBox1.Size.Height);
- }
- //BUTTON 1 - Add a song
- private void button1_Click(object sender, EventArgs e)
- {
- Song song = new Song();
- if (song.success)
- {
- songs[index] = song;
- index++;
- Reload();
- }
- }
- //BUTTON 6 - Remove a song
- private void button6_Click(object sender, EventArgs e)
- {
- //Song index to remove
- int songToRemove = listBox1.SelectedIndex;
- //Double Check
- DialogResult dialogResult = MessageBox.Show("Do you want to remove the song: " + songs[songToRemove].name, "Remove Song?", MessageBoxButtons.YesNo);
- if(dialogResult == DialogResult.Yes)
- {
- //If the song to remove is an actual song
- if (songToRemove != -1)
- {
- //Remove it from the list
- listBox1.Items.RemoveAt(songToRemove);
- //Create a new writer
- using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"Songs.txt", false))
- {
- //New song array to put in text file
- string[] dirs = new string[128];
- //Go through all songs
- for (int i = 0; i < songs.Length; i++)
- {
- //If the song exists and it is not the song to remove, if it is then skip it.
- if (songs[i] != null && i != songToRemove)
- {
- //Write it to the new file
- dirs[i] = songs[i].directory;
- file.WriteLine(dirs[i]);
- }
- }
- }
- }
- }
- //Reload songs and stop player
- if (dialogResult == DialogResult.Yes)
- {
- player.Ctlcontrols.stop();
- Reload();
- }
- }
- //BUTTON 2 - Play Button
- private void button2_Click(object sender, EventArgs e)
- {
- if(listBox1.SelectedIndex <= songs.Length && listBox1.SelectedIndex >= 0 && songs[listBox1.SelectedIndex] != null && changeWanted)
- {
- //Set new song up
- player.URL = songs[listBox1.SelectedIndex].directory;
- playing = listBox1.SelectedIndex;
- //Stop it from autoplaying
- // player.Ctlcontrols.stop();
- //Play
- player.Ctlcontrols.play();
- //Start the current second timer.
- timer2.Start();
- //Delay timer1 for 2 sec, because it does not work otherwise.
- timer3.Start();
- changeWanted = false;
- } else
- {
- player.Ctlcontrols.play();
- }
- }
- //BUTTON 4 - Pause button
- private void button4_Click(object sender, EventArgs e)
- {
- player.Ctlcontrols.pause();
- }
- //BUTTON 5 - Skip ten seconds of the song
- private void button5_Click(object sender, EventArgs e)
- {
- if (player.currentMedia != null && player.Ctlcontrols.currentPosition + 10 < player.currentMedia.duration)
- {
- player.Ctlcontrols.currentPosition += 10;
- }
- }
- //BUTTON 3 - Go back ten seconds
- private void button3_Click(object sender, EventArgs e)
- {
- if (player.currentMedia != null && player.Ctlcontrols.currentPosition - 10 > 10)
- {
- player.Ctlcontrols.currentPosition -= 10;
- } else if (player.currentMedia != null && (int)player.Ctlcontrols.currentPosition <= 10)
- {
- player.Ctlcontrols.currentPosition = 0;
- }
- }
- //BUTTON 7 - Shuffle
- private void button7_Click(object sender, EventArgs e)
- {
- shuffleActivated = !shuffleActivated;
- shuffle = new Shuffle(ref songs);
- if (shuffleActivated)
- {
- player.Ctlcontrols.stop();
- player.URL = songs[shuffle.currentSong].directory;
- listBox1.SetSelected(shuffle.currentSong, true);
- //Start the current second timer.
- timer2.Start();
- //Delay timer1 for 2 sec, because it does not work otherwise.
- timer3.Start();
- timer4.Start();
- }
- }
- //BUTTON 8 - SKIP
- private void button8_Click(object sender, EventArgs e)
- {
- playing++;
- if (shuffleActivated)
- {
- shuffle.Next(ref player);
- player.URL = songs[shuffle.currentSong].directory;
- listBox1.SetSelected(shuffle.currentSong, true);
- player.Ctlcontrols.play();
- return;
- }
- if(songs[playing] == null)
- {
- playing--;
- } else
- {
- player.URL = songs[playing].directory;
- player.Ctlcontrols.play();
- listBox1.SetSelected(playing, true);
- }
- }
- //BUTTON 9 - PREVIOUS
- private void button9_Click(object sender, EventArgs e)
- {
- doublePrevClicks++;
- if(doublePrevClicks >= 2)
- {
- playing--;
- if(playing < 0)
- {
- playing = 0;
- } else
- {
- player.URL = songs[playing].directory;
- player.Ctlcontrols.play();
- listBox1.SetSelected(playing, true);
- doublePrevClicks = 0;
- }
- }
- else
- {
- player.Ctlcontrols.stop();
- player.Ctlcontrols.play();
- }
- }
- //BUTTON 10 - +60 seconds
- private void button10_Click(object sender, EventArgs e)
- {
- if (player.currentMedia != null && player.Ctlcontrols.currentPosition + 60 < player.currentMedia.duration)
- {
- player.Ctlcontrols.currentPosition += 60;
- }
- }
- private void button11_Click(object sender, EventArgs e)
- {
- if (player.currentMedia != null && player.Ctlcontrols.currentPosition - 60 > 0)
- {
- player.Ctlcontrols.currentPosition -= 60;
- }
- else if (player.currentMedia != null && (int)player.Ctlcontrols.currentPosition <= 60)
- {
- player.Ctlcontrols.currentPosition = 0;
- }
- }
- //LISTBOX -- If a new song was selected
- private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
- {
- //If its not -1 then the click was on an actual song
- if (listBox1.SelectedIndex != -1 && listBox1.SelectedIndex != lastIndex)
- {
- //Set last index to stop it from playing again when clicked
- lastIndex = listBox1.SelectedIndex;
- playing = listBox1.SelectedIndex;
- changeWanted = true;
- }
- }
- //TRACKBAR - Volume slider.
- private void trackBar1_Scroll(object sender, EventArgs e)
- {
- player.settings.volume = trackBar1.Value;
- }
- //TIMER - Update progress bar
- private void timer1_Tick(object sender, EventArgs e)
- {
- if (player.currentMedia != null)
- {
- //Set max size for progress bar
- progressBar1.Maximum = (int)player.currentMedia.duration;
- //Current pos on progress
- progressBar1.Value = (int)player.Ctlcontrols.currentPosition;
- //Update progressbar
- progressBar1.Update();
- //If the song is over stop the timer.
- if ((int)player.currentMedia.duration >= (int)player.Ctlcontrols.currentPosition)
- {
- timer1.Stop();
- }
- }
- }
- //TIMER - This updates song stats, current time(s) and duration(s)
- private void timer2_Tick(object sender, EventArgs e)
- {
- if (player.currentMedia != null)
- {
- //make them int so it looks nice
- int currentSec = (int)player.Ctlcontrols.currentPosition;
- int duration = (int)player.currentMedia.duration;
- //Update text
- label1.Text = duration.ToString() + " s";
- label2.Text = currentSec.ToString() + " s";
- }
- }
- //TIMER - Delay progress bar launch
- private void timer3_Tick(object sender, EventArgs e)
- {
- timer1.Start();
- }
- //SHUFFLE TIMER
- private void timer4_Tick(object sender, EventArgs e)
- {
- if (shuffleActivated)
- {
- if ((int)player.Ctlcontrols.currentPosition == (int)player.currentMedia.duration - 1)
- {
- shuffle.Next(ref player);
- player.URL = songs[shuffle.currentSong].directory;
- listBox1.SetSelected(shuffle.currentSong, true);
- player.Ctlcontrols.play();
- }
- }
- }
- //Reload all songs from file
- void Reload()
- {
- index = 0;
- lastIndex = -1;
- //Reset songs array
- for(int i = 0; i < songs.Length; i++)
- {
- if(songs[i] != null)
- {
- songs[i] = null;
- }
- }
- //ReRead the text file and create new array
- //create text file if it doesnt exist, if it does load all the songs in.
- if (System.IO.File.Exists(@"Songs.txt"))
- {
- string[] lines = System.IO.File.ReadAllLines(@"Songs.txt");
- foreach (string line in lines)
- {
- Song song = new Song(line);
- songs[index] = song;
- index++;
- }
- }
- else
- {
- System.IO.File.Create(@"Songs.txt");
- }
- //Clear listbox
- listBox1.Items.Clear();
- //Create list of songs on screen
- foreach (Song a in songs)
- {
- if (a != null)
- listBox1.Items.Add(a.name);
- }
- }
- //Shuffle Class
- public class Shuffle
- {
- public int currentSong;
- int[] songList;
- int songCount;
- Random random;
- int index;
- public void Next(ref AxWMPLib.AxWindowsMediaPlayer player)
- {
- if(index == songList.Length - 1)
- {
- player.Ctlcontrols.stop();
- return;
- }
- if (index < songList.Length)
- {
- currentSong = songList[++index];
- }
- }
- void reshuffle(int[] indexes)
- {
- // Knuth shuffle algorithm :: courtesy of Wikipedia :)
- for (int t = 0; t < indexes.Length; t++)
- {
- int tmp = indexes[t];
- int r = random.Next(t, indexes.Length - 1);
- indexes[t] = indexes[r];
- indexes[r] = tmp;
- }
- }
- public Shuffle(ref Song[] songs)
- {
- index = 0;
- random = new Random();
- //Get song count
- for (int i = 0; i < songs.Length; i++)
- {
- if(songs[i] != null)
- {
- songCount++;
- }
- }
- songList = new int[songCount];
- for (int i = 0; i < songCount; i++)
- {
- songList[i] = i;
- }
- reshuffle(songList);
- currentSong = songList[index];
- Console.WriteLine("ORDER OF SHUFFLE: ");
- for(int i = 0; i < songList.Length; i++)
- {
- Console.WriteLine(songList[i]);
- }
- }
- }
- //Song Class
- public class Song
- {
- //Variables for a song
- public string name;
- public string directory;
- public bool success = false;
- public bool playing = false;
- public Song()
- {
- //For opening a file
- OpenFileDialog openFile = new OpenFileDialog();
- //Show windows explorer and open file
- if (openFile.ShowDialog() == DialogResult.OK)
- {
- //Init variables
- directory = openFile.FileName;
- name = openFile.SafeFileName;
- //save the songs address to the list
- using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"Songs.txt", true))
- {
- file.WriteLine(directory);
- }
- success = true;
- }
- else
- {
- success = false;
- }
- }
- //Alternative constructor for a song. This is for loading in a song from the text file.
- public Song(string directory)
- {
- OpenFileDialog openFile = new OpenFileDialog();
- openFile.FileName = directory;
- this.name = openFile.SafeFileName;
- this.directory = directory;
- }
- }
- //UNUSED STUFF
- //Duration of the song
- private void label1_Click(object sender, EventArgs e)
- {
- }
- //currentSec
- private void label2_Click(object sender, EventArgs e)
- {
- }
- //Progress bar showing how far the song is
- public void progressBar1_Click(object sender, EventArgs e)
- {
- }
- //Unnecessary
- private void Form1_Load(object sender, EventArgs e)
- {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement