| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526 |
- using NAudio.Wave;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Reflection;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace MusicPlayer
- {
- public partial class MainForm : Form
- {
- bool songFinished;
- bool draggedstarted = false;
- bool draggedcompleted = false;
- int startx = 0;
- int starty = 0;
- public Main main
- {
- get; set;
- }
- public MainForm()
- {
- InitializeComponent();
- PositionTrackBar.Scroll += (s, e) =>
- {
- this.PositionTrackBar_ValueChanged();
- };
- PositionTrackBar.MouseDown += (s, e) =>
- {
- clicked = true;
- };
- PositionTrackBar.MouseUp += (s, e) =>
- {
- if (!clicked)
- return;
- clicked = false;
- this.PositionTrackBar_ValueChanged();
- };
- songFinished = false;
- }
- private void MainForm_Load(object sender, EventArgs e)
- {
- UpdateTimer.Start();
- }
- private void PlayButton_Click(object sender, EventArgs e)
- {
- if (main.audio.AState == AudioHandler.AudioState.PAUSED)
- main.audio.Play(main.audio.CurrentSong);
- else
- SongsTableView_CellDoubleClick(sender, new DataGridViewCellEventArgs(0, 0));
- }
- private void PauseButton_Click(object sender, EventArgs e)
- {
- main.audio.Pause();
- }
- private void StopButton_Click(object sender, EventArgs e)
- {
- main.audio.Stop();
- }
- public void SongFinished()
- {
- songFinished = true;
- }
- private void UpdateTimer_Tick(object sender, EventArgs e)
- {
- //Trackbar
- if (main.audio.BState == AudioHandler.BufferState.DONE)
- PositionTrackBar.Enabled = true;
- else
- PositionTrackBar.Enabled = false;
- if (!clicked)
- PositionTrackBar.Value = Math.Max(main.audio.Position, 0);
- //Buffer display
- BufferBar.Value = main.audio.Buffered / 10;
- //Time labels
- if (!clicked)
- LabelCurrentTime.Text = Main.SecondsToTimestamp(main.audio.CurrentTime);
- LabelTotalTime.Text = Main.SecondsToTimestamp(main.audio.TotalTime);
- //Current song label
- if (main.audio.CurrentSong == null)
- CurrentSongLabel.Text = "Not playing any songs";
- else
- {
- if (main.audio.CurrentSong.Seconds < 1)
- PositionTrackBar.Enabled = false;
- CurrentSongLabel.Text = "Currently playing: " + main.audio.CurrentSong.Name;
- }
- //Buttons and context menu
- if (main.audio.AState == AudioHandler.AudioState.PLAYING)
- {
- PlayButton.Enabled = false;
- NotifyMenuStripPlayButton.Enabled = false;
- NotifyMenuStripPlayingLabel.Text = "Playing";
- NotifyMenuStripPlayingLabel.Enabled = true;
- NotifyMenuStripPlayingSongLabel.Visible = true;
- NotifyMenuStripPlayingSongLabel.Text = main.audio.CurrentSong.Name;
- }
- else
- {
- PlayButton.Enabled = true;
- NotifyMenuStripPlayButton.Enabled = true;
- NotifyMenuStripPlayingSongLabel.Visible = false;
- }
- if (main.nw.ip == "http://jancokock.me")
- SelectServerJancoButton.Enabled = false;
- else
- SelectServerJancoButton.Enabled = true;
- if (main.nw.ip == "http://imegumii.nl")
- SelectServerYorickButton.Enabled = false;
- else
- SelectServerYorickButton.Enabled = true;
- if (main.audio.AState == AudioHandler.AudioState.PAUSED)
- {
- PauseButton.Enabled = false;
- NotifyMenuStripPauseButton.Enabled = false;
- NotifyMenuStripPlayingLabel.Text = "Paused";
- NotifyMenuStripPlayingLabel.Enabled = true;
- NotifyMenuStripPlayingSongLabel.Visible = true;
- NotifyMenuStripPlayingSongLabel.Text = main.audio.CurrentSong.Name;
- }
- else
- {
- PauseButton.Enabled = true;
- NotifyMenuStripPauseButton.Enabled = true;
- }
- if (main.audio.AState == AudioHandler.AudioState.STOPPED)
- {
- StopButton.Enabled = false;
- NotifyMenuStripStopButton.Enabled = false;
- NotifyMenuStripPlayingLabel.Text = "Stopped";
- NotifyMenuStripPlayingLabel.Enabled = false;
- NotifyMenuStripPlayingSongLabel.Visible = false;
- NotifyMenuStripPlayingSongLabel.Text = "Stopped";
- }
- else
- {
- StopButton.Enabled = true;
- NotifyMenuStripStopButton.Enabled = true;
- }
- if(PlayNextSongButton.Checked)
- {
- ShuffleSongButton.Enabled = true;
- }
- else
- {
- ShuffleSongButton.Enabled = false;
- ShuffleSongButton.Checked = false;
- }
- if (main.currentPlayingList.Count <= 1)
- {
- PreviousButton.Enabled = false;
- NextButton.Enabled = false;
- NotifyMenuStripPreviousButton.Enabled = false;
- NotifyMenuStripNextButton.Enabled = false;
- }
- else
- {
- NextButton.Enabled = true;
- NotifyMenuStripNextButton.Enabled = true;
- if (ShuffleSongButton.Checked)
- {
- PreviousButton.Enabled = false;
- NotifyMenuStripPreviousButton.Enabled = false;
- }
- else
- {
- PreviousButton.Enabled = true;
- NotifyMenuStripPreviousButton.Enabled = true;
- }
- }
- if (songFinished)
- {
- Thread.Sleep(20);
- if (PlayNextSongButton.Checked)
- {
- NextButton_Click(this, new EventArgs());
- }
- else if (LoopSongButton.Checked)
- {
- main.audio.Play(main.audio.CurrentSong);
- }
- songFinished = false;
- }
- }
- private void GenreListBox_SelectedIndexChanged(object sender, EventArgs e)
- {
- if (GenreListBox.SelectedItems.Count != 0)
- {
- main.GenreFilter(GenreListBox.SelectedItems[0].ToString());
- ArtistListBox.ClearSelected();
- PlaylistBox.ClearSelected();
- AlbumListView.SelectedIndices.Clear();
- }
- }
- private void PlaylistBox_SelectedIndexChanged(object sender, EventArgs e)
- {
- if (PlaylistBox.SelectedItems.Count != 0)
- {
- main.PlaylistFilter(PlaylistBox.SelectedItems[0].ToString());
- GenreListBox.ClearSelected();
- ArtistListBox.ClearSelected();
- AlbumListView.SelectedIndices.Clear();
- }
- }
- private void ArtistListBox_SelectedIndexChanged(object sender, EventArgs e)
- {
- if (ArtistListBox.SelectedItems.Count != 0)
- {
- main.ArtistFilter(ArtistListBox.SelectedItems[0].ToString());
- GenreListBox.ClearSelected();
- PlaylistBox.ClearSelected();
- AlbumListView.SelectedIndices.Clear();
- }
- }
- private void AlbumListView_SelectedIndexChanged(object sender, EventArgs e)
- {
- if (AlbumListView.SelectedItems.Count != 0)
- {
- main.AlbumFilter(AlbumListView.SelectedItems[0].Text);
- PlaylistBox.ClearSelected();
- ArtistListBox.ClearSelected();
- GenreListBox.ClearSelected();
- }
- }
- private void SongsTableView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
- {
- if (e.RowIndex != -1)
- {
- SongsTable s = new SongsTable();
- if (SongsTableView.SelectedRows.Count > 0)
- {
- main.currentPlayingList = main.table.AsEnumerable().Select(x => x[5] as Song).ToList();
- var drv = SongsTableView.SelectedRows[0].DataBoundItem as DataRowView;
- var row = drv.Row as DataRow;
- s.ImportRow(row);
- main.audio.Play((s.Rows[0][5] as Song));
- }
- }
- }
- private void PositionTrackBar_ValueChanged()
- {
- if (!clicked)
- main.audio.Seek(PositionTrackBar.Value);
- LabelCurrentTime.Text = Main.SecondsToTimestamp((int)(((double)PositionTrackBar.Value / 1000) * main.audio.CurrentSong.Seconds));
- }
- private void NotifyIcon_Click(object sender, MouseEventArgs e)
- {
- if (e.Button == MouseButtons.Left)
- {
- MethodInfo mi = typeof(NotifyIcon).GetMethod("ShowContextMenu", BindingFlags.Instance | BindingFlags.NonPublic);
- mi.Invoke(NotifyIcon, null);
- }
- }
- private void overviewToolStripMenuItem_Click(object sender, EventArgs e)
- {
- this.PlaylistBox.Visible = false;
- this.GenreListBox.Visible = true;
- this.ArtistListBox.Visible = true;
- this.AlbumListView.Visible = true;
- this.GenreListLabel.Visible = true;
- this.AlbumListLabel.Visible = true;
- this.ArtistListLabel.Visible = true;
- this.PlaylistListLabel.Visible = false;
- }
- private void playlistsToolStripMenuItem_Click(object sender, EventArgs e)
- {
- this.PlaylistBox.Visible = true;
- this.GenreListBox.Visible = false;
- this.ArtistListBox.Visible = false;
- this.AlbumListView.Visible = false;
- this.GenreListLabel.Visible = false;
- this.AlbumListLabel.Visible = false;
- this.ArtistListLabel.Visible = false;
- this.PlaylistListLabel.Visible = true;
- }
- private void exitToolStripMenuItem_Click(object sender, EventArgs e)
- {
- ExitProgram();
- }
- private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
- {
- ExitProgram();
- }
- private void ExitProgram()
- {
- main.audio.AState = AudioHandler.AudioState.STOPPED;
- NotifyIcon.Visible = false;
- NotifyIcon.Icon = null;
- System.Windows.Forms.Application.Exit();
- }
- private void NotifyMenuStripPlayButton_Click(object sender, EventArgs e)
- {
- PlayButton_Click(sender, e);
- }
- private void NotifyMenuStripPauseButton_Click(object sender, EventArgs e)
- {
- PauseButton_Click(sender, e);
- }
- private void NotifyMenuStripStopButton_Click(object sender, EventArgs e)
- {
- StopButton_Click(sender, e);
- }
- private void makeToolStripMenuItem_Click(object sender, EventArgs e)
- {
- PlaylistMaker p = new PlaylistMaker(main.pl, main.api);
- p.ShowDialog();
- main.Repopulate();
- }
- private void SearchArtistsTextBox_KeyUp(object sender, KeyEventArgs e)
- {
- main.SearchArtist(SearchArtistsTextBox.Text);
- if (SearchArtistsTextBox.Text.Length < 1)
- ClearArtistSearchButton.Enabled = false;
- else
- ClearArtistSearchButton.Enabled = true;
- }
- private void SearchGenresTextBox_KeyUp(object sender, KeyEventArgs e)
- {
- main.SearchGenre(SearchGenresTextBox.Text);
- if (SearchGenresTextBox.Text.Length < 1)
- ClearGenreSearchButton.Enabled = false;
- else
- ClearGenreSearchButton.Enabled = true;
- }
- private void ClearArtistSearchButton_Click(object sender, EventArgs e)
- {
- main.SearchArtist("");
- SearchArtistsTextBox.Text = "";
- ClearArtistSearchButton.Enabled = false;
- }
- private void ClearGenreSearchButton_Click(object sender, EventArgs e)
- {
- main.SearchGenre("");
- SearchGenresTextBox.Text = "";
- ClearGenreSearchButton.Enabled = false;
- }
- private void PositionTrackBar_MouseDown(object sender, MouseEventArgs e)
- {
- double dblValue;
- dblValue = ((double)(e.X+PositionTrackBar.Location.X) / (double)(PositionTrackBar.Width + PositionTrackBar.Location.X)) * (PositionTrackBar.Maximum - PositionTrackBar.Minimum);
- PositionTrackBar.Value = Convert.ToInt32(dblValue);
- }
- private void PreviousButton_Click(object sender, EventArgs e)
- {
- int selected = 0;
- selected = main.currentPlayingList.IndexOf(main.audio.CurrentSong) - 1;
- if (selected < 0)
- {
- if (LoopSongButton.Checked)
- selected = main.currentPlayingList.Count-1;
- }
- main.FilterCurrentPlaying();
- SongsTableView.CurrentCell = SongsTableView.Rows[selected].Cells[0];
- main.audio.Play(main.currentPlayingList[selected]);
- }
- private void NextButton_Click(object sender, EventArgs e)
- {
- int selected = 0;
- if (ShuffleSongButton.Checked)
- {
- Random r = new Random();
- selected = r.Next(0, main.currentPlayingList.Count);
- while (selected == main.currentPlayingList.IndexOf(main.audio.CurrentSong) && main.currentPlayingList.Count > 1)
- selected = r.Next(0, main.currentPlayingList.Count);
- }
- else
- selected = main.currentPlayingList.IndexOf(main.audio.CurrentSong) + 1;
- if (selected >= main.currentPlayingList.Count)
- {
- if (LoopSongButton.Checked)
- selected = 0;
- else
- {
- songFinished = false;
- return;
- }
- }
- main.FilterCurrentPlaying();
- SongsTableView.CurrentCell = SongsTableView.Rows[selected].Cells[0];
- main.audio.Play(main.currentPlayingList[selected]);
- }
- private void NotifyMenuStripNextButton_Click(object sender, EventArgs e)
- {
- NextButton_Click(sender, e);
- }
- private void NotifyMenuStripPreviousButton_Click(object sender, EventArgs e)
- {
- PreviousButton_Click(sender, e);
- }
- private void ViewCurrentPlaylistButton_Click(object sender, EventArgs e)
- {
- main.FilterCurrentPlaying();
- int selected = main.currentPlayingList.IndexOf(main.audio.CurrentSong);
- if(main.currentPlayingList.Count >= 1)
- SongsTableView.CurrentCell = SongsTableView.Rows[selected].Cells[0];
- }
-
- private void SongsTableView_MouseDown(object sender, MouseEventArgs e)
- {
- draggedstarted = true;
- draggedcompleted = false;
- startx = e.X;
- starty = e.Y;
- }
- private void SongsTableView_MouseUp(object sender, MouseEventArgs e)
- {
- if (draggedcompleted)
- {
- Cursor.Current = Cursors.Default;
- Point point = PlaylistBox.PointToClient(Cursor.Position);
- int index = PlaylistBox.IndexFromPoint(point);
- if (index < 0) //nope, niet op een playlist gesleept
- {
- draggedstarted = false;
- draggedcompleted = false;
- return;
- }
- Playlist currentPlaylist = main.pl.GetPlaylistByName(PlaylistBox.Items[index].ToString());
- SongsTable s = new SongsTable();
- if (SongsTableView.SelectedRows.Count > 0)
- {
- var drv = SongsTableView.SelectedRows[0].DataBoundItem as DataRowView;
- var row = drv.Row as DataRow;
- s.ImportRow(row);
- currentPlaylist.AddSong((s.Rows[0][5] as Song));
- currentPlaylist.WriteToFile();
- }
- }
- draggedcompleted = false;
- draggedstarted = false;
- }
- private void SongsTableView_MouseMove(object sender, MouseEventArgs e)
- {
- int deltax = Math.Abs(startx - e.X);
- int deltay = Math.Abs(starty - e.Y);
- if ((deltax > 5 || deltay > 5) && draggedstarted && !draggedcompleted)
- {
- draggedcompleted = true;
- playlistsToolStripMenuItem_Click(this, new EventArgs());
- Cursor.Current = Cursors.Hand;
- }
- }
- private void SelectServerJancoButton_Click(object sender, EventArgs e)
- {
- main.SwitchServer("http://jancokock.me");
- }
- private void SelectServerYorickButton_Click(object sender, EventArgs e)
- {
- main.SwitchServer("http://imegumii.nl");
- }
- }
- }
|