From 87404b0f852de8bd5aa605d6bcc58086fbe84b05 Mon Sep 17 00:00:00 2001 From: Kenneth van Ewijk Date: Sat, 31 Oct 2015 11:57:05 +0100 Subject: [PATCH] Implemented play next, shuffle and loop. Implemented Artist and Genre search --- MusicPlayer/MusicPlayer/AudioHandler.cs | 9 +- MusicPlayer/MusicPlayer/Main.cs | 59 +++++++- MusicPlayer/MusicPlayer/MainForm.Designer.cs | 149 +++++++++++++++++-- MusicPlayer/MusicPlayer/MainForm.cs | 87 +++++++++++ 4 files changed, 286 insertions(+), 18 deletions(-) diff --git a/MusicPlayer/MusicPlayer/AudioHandler.cs b/MusicPlayer/MusicPlayer/AudioHandler.cs index 42ded46..0f8e05d 100755 --- a/MusicPlayer/MusicPlayer/AudioHandler.cs +++ b/MusicPlayer/MusicPlayer/AudioHandler.cs @@ -40,9 +40,12 @@ namespace MusicPlayer public Song CurrentSong; - public AudioHandler() + private Main main; + + public AudioHandler(Main main) { - CreateThreads(); + this.main = main; + CreateThreads(); } private void CreateThreads() @@ -171,6 +174,8 @@ namespace MusicPlayer } + if(AState == AudioState.PLAYING) + main.form.SongFinished(); AState = AudioState.STOPPED; playpos = 0; CurrentTime = 0; diff --git a/MusicPlayer/MusicPlayer/Main.cs b/MusicPlayer/MusicPlayer/Main.cs index eb6e2c1..fd4daf5 100644 --- a/MusicPlayer/MusicPlayer/Main.cs +++ b/MusicPlayer/MusicPlayer/Main.cs @@ -18,6 +18,9 @@ namespace MusicPlayer private SongsTable table; private ImageList imagelist; + private List genres; + private List artists; + public Main(NetworkHandler nw, APIHandler api, MainForm form, PlaylistHandler pl) { this.nw = nw; @@ -26,11 +29,15 @@ namespace MusicPlayer form.main = this; this.pl = pl; - audio = new AudioHandler(); + audio = new AudioHandler(this); table = new SongsTable(); imagelist = new ImageList(); form.SongsTableView.DataSource = table; form.SongsTableView.Columns[5].Visible = false; + + genres = new List(); + artists = new List(); + Populate(); } @@ -38,9 +45,9 @@ namespace MusicPlayer { form.AlbumListView.LargeImageList = imagelist; this.api.GetAlbums().ForEach(a => { var item = form.AlbumListView.Items.Add(a.albumnaam); imagelist.Images.Add(a.albumnaam, a.cover); item.ImageKey = a.albumnaam;}); - this.api.GetArtists().ForEach(a => form.ArtistListBox.Items.Add(a.naam)); - this.api.GetGenres().ForEach(g => form.GenreListBox.Items.Add(g.name)); - this.pl.GetPlaylists().ForEach(p => form.PlaylistBox.Items.Add(p.name)); + this.api.GetArtists().ForEach(a => { artists.Add(a.naam); form.ArtistListBox.Items.Add(a.naam); }); + this.api.GetGenres().ForEach(g => { genres.Add(g.name); form.GenreListBox.Items.Add(g.name); }); + this.pl.GetPlaylists().ForEach(p => form.PlaylistBox.Items.Add(p.name)); } public void Repopulate() @@ -64,6 +71,50 @@ namespace MusicPlayer }); } + public void SearchArtist(string search) + { + form.ArtistListBox.Items.Clear(); + + if (search.Length > 1) + { + string sPattern = search; + + foreach (string s in artists) + { + if (System.Text.RegularExpressions.Regex.IsMatch(s, sPattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase)) + { + form.ArtistListBox.Items.Add(s); + } + } + } + else + { + artists.ForEach(a => form.ArtistListBox.Items.Add(a)); + } + } + + public void SearchGenre(string search) + { + form.GenreListBox.Items.Clear(); + + if (search.Length > 1) + { + string sPattern = search; + + foreach (string s in genres) + { + if (System.Text.RegularExpressions.Regex.IsMatch(s, sPattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase)) + { + form.GenreListBox.Items.Add(s); + } + } + } + else + { + genres.ForEach(g => form.GenreListBox.Items.Add(g)); + } + } + public void GenreFilter(string genre) { table.Clear(); diff --git a/MusicPlayer/MusicPlayer/MainForm.Designer.cs b/MusicPlayer/MusicPlayer/MainForm.Designer.cs index c9638c9..37ea3c3 100644 --- a/MusicPlayer/MusicPlayer/MainForm.Designer.cs +++ b/MusicPlayer/MusicPlayer/MainForm.Designer.cs @@ -1,4 +1,6 @@ -namespace MusicPlayer +using System; + +namespace MusicPlayer { partial class MainForm { @@ -48,10 +50,22 @@ this.viewToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.overviewToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.playlistsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.playbackToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.PlayNextSongButton = new System.Windows.Forms.ToolStripMenuItem(); + this.LoopSongButton = new System.Windows.Forms.ToolStripMenuItem(); + this.ShuffleSongButton = new System.Windows.Forms.ToolStripMenuItem(); this.playlistToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.makeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.toolStripSeparator3 = new System.Windows.Forms.ToolStripSeparator(); this.saveToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem(); this.loadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); - this.makeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.searchToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.SearchGenresToolStripLabel = new System.Windows.Forms.ToolStripMenuItem(); + this.SearchGenresTextBox = new System.Windows.Forms.ToolStripTextBox(); + this.ClearGenreSearchButton = new System.Windows.Forms.ToolStripMenuItem(); + this.SearchArtistToolStripLabel = new System.Windows.Forms.ToolStripMenuItem(); + this.SearchArtistsTextBox = new System.Windows.Forms.ToolStripTextBox(); + this.ClearArtistSearchButton = new System.Windows.Forms.ToolStripMenuItem(); this.ControlsPanel = new System.Windows.Forms.Panel(); this.CurrentSongLabel = new System.Windows.Forms.Label(); this.SeperatorLabel = new System.Windows.Forms.Label(); @@ -241,7 +255,9 @@ this.MenuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.fileToolStripMenuItem, this.viewToolStripMenuItem, - this.playlistToolStripMenuItem}); + this.playbackToolStripMenuItem, + this.playlistToolStripMenuItem, + this.searchToolStripMenuItem}); this.MenuStrip.Location = new System.Drawing.Point(0, 0); this.MenuStrip.Name = "MenuStrip"; this.MenuStrip.Size = new System.Drawing.Size(784, 24); @@ -292,34 +308,131 @@ this.playlistsToolStripMenuItem.Text = "Playlists"; this.playlistsToolStripMenuItem.Click += new System.EventHandler(this.playlistsToolStripMenuItem_Click); // + // playbackToolStripMenuItem + // + this.playbackToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.PlayNextSongButton, + this.LoopSongButton, + this.ShuffleSongButton}); + this.playbackToolStripMenuItem.Name = "playbackToolStripMenuItem"; + this.playbackToolStripMenuItem.Size = new System.Drawing.Size(66, 20); + this.playbackToolStripMenuItem.Text = "Playback"; + // + // PlayNextSongButton + // + this.PlayNextSongButton.Checked = true; + this.PlayNextSongButton.CheckOnClick = true; + this.PlayNextSongButton.CheckState = System.Windows.Forms.CheckState.Checked; + this.PlayNextSongButton.Name = "PlayNextSongButton"; + this.PlayNextSongButton.Size = new System.Drawing.Size(152, 22); + this.PlayNextSongButton.Text = "Play Next"; + // + // LoopSongButton + // + this.LoopSongButton.Checked = true; + this.LoopSongButton.CheckOnClick = true; + this.LoopSongButton.CheckState = System.Windows.Forms.CheckState.Checked; + this.LoopSongButton.Name = "LoopSongButton"; + this.LoopSongButton.Size = new System.Drawing.Size(152, 22); + this.LoopSongButton.Text = "Loop"; + // + // ShuffleSongButton + // + this.ShuffleSongButton.CheckOnClick = true; + this.ShuffleSongButton.Enabled = false; + this.ShuffleSongButton.Name = "ShuffleSongButton"; + this.ShuffleSongButton.Size = new System.Drawing.Size(152, 22); + this.ShuffleSongButton.Text = "Shuffle"; + // // playlistToolStripMenuItem // this.playlistToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.makeToolStripMenuItem, + this.toolStripSeparator3, this.saveToolStripMenuItem1, - this.loadToolStripMenuItem, - this.makeToolStripMenuItem}); + this.loadToolStripMenuItem}); this.playlistToolStripMenuItem.Name = "playlistToolStripMenuItem"; this.playlistToolStripMenuItem.Size = new System.Drawing.Size(56, 20); this.playlistToolStripMenuItem.Text = "Playlist"; // + // makeToolStripMenuItem + // + this.makeToolStripMenuItem.Name = "makeToolStripMenuItem"; + this.makeToolStripMenuItem.Size = new System.Drawing.Size(139, 22); + this.makeToolStripMenuItem.Text = "Create / Edit"; + this.makeToolStripMenuItem.Click += new System.EventHandler(this.makeToolStripMenuItem_Click); + // + // toolStripSeparator3 + // + this.toolStripSeparator3.Name = "toolStripSeparator3"; + this.toolStripSeparator3.Size = new System.Drawing.Size(136, 6); + // // saveToolStripMenuItem1 // this.saveToolStripMenuItem1.Name = "saveToolStripMenuItem1"; - this.saveToolStripMenuItem1.Size = new System.Drawing.Size(103, 22); + this.saveToolStripMenuItem1.Size = new System.Drawing.Size(139, 22); this.saveToolStripMenuItem1.Text = "Save"; // // loadToolStripMenuItem // this.loadToolStripMenuItem.Name = "loadToolStripMenuItem"; - this.loadToolStripMenuItem.Size = new System.Drawing.Size(103, 22); + this.loadToolStripMenuItem.Size = new System.Drawing.Size(139, 22); this.loadToolStripMenuItem.Text = "Load"; // - // makeToolStripMenuItem + // searchToolStripMenuItem // - this.makeToolStripMenuItem.Name = "makeToolStripMenuItem"; - this.makeToolStripMenuItem.Size = new System.Drawing.Size(152, 22); - this.makeToolStripMenuItem.Text = "Create / Edit"; - this.makeToolStripMenuItem.Click += new System.EventHandler(this.makeToolStripMenuItem_Click); + this.searchToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.SearchGenresToolStripLabel, + this.SearchArtistToolStripLabel}); + this.searchToolStripMenuItem.Name = "searchToolStripMenuItem"; + this.searchToolStripMenuItem.Size = new System.Drawing.Size(54, 20); + this.searchToolStripMenuItem.Text = "Search"; + // + // SearchGenresToolStripLabel + // + this.SearchGenresToolStripLabel.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.SearchGenresTextBox, + this.ClearGenreSearchButton}); + this.SearchGenresToolStripLabel.Name = "SearchGenresToolStripLabel"; + this.SearchGenresToolStripLabel.Size = new System.Drawing.Size(110, 22); + this.SearchGenresToolStripLabel.Text = "Genres"; + // + // SearchGenresTextBox + // + this.SearchGenresTextBox.Name = "SearchGenresTextBox"; + this.SearchGenresTextBox.Size = new System.Drawing.Size(100, 23); + this.SearchGenresTextBox.KeyUp += new System.Windows.Forms.KeyEventHandler(this.SearchGenresTextBox_KeyUp); + // + // ClearGenreSearchButton + // + this.ClearGenreSearchButton.Enabled = false; + this.ClearGenreSearchButton.Name = "ClearGenreSearchButton"; + this.ClearGenreSearchButton.Size = new System.Drawing.Size(160, 22); + this.ClearGenreSearchButton.Text = "Clear Search"; + this.ClearGenreSearchButton.Click += new System.EventHandler(this.ClearGenreSearchButton_Click); + // + // SearchArtistToolStripLabel + // + this.SearchArtistToolStripLabel.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.SearchArtistsTextBox, + this.ClearArtistSearchButton}); + this.SearchArtistToolStripLabel.Name = "SearchArtistToolStripLabel"; + this.SearchArtistToolStripLabel.Size = new System.Drawing.Size(110, 22); + this.SearchArtistToolStripLabel.Text = "Artists"; + // + // SearchArtistsTextBox + // + this.SearchArtistsTextBox.Name = "SearchArtistsTextBox"; + this.SearchArtistsTextBox.Size = new System.Drawing.Size(100, 23); + this.SearchArtistsTextBox.KeyUp += new System.Windows.Forms.KeyEventHandler(this.SearchArtistsTextBox_KeyUp); + // + // ClearArtistSearchButton + // + this.ClearArtistSearchButton.Enabled = false; + this.ClearArtistSearchButton.Name = "ClearArtistSearchButton"; + this.ClearArtistSearchButton.Size = new System.Drawing.Size(160, 22); + this.ClearArtistSearchButton.Text = "Clear Search"; + this.ClearArtistSearchButton.Click += new System.EventHandler(this.ClearArtistSearchButton_Click); // // ControlsPanel // @@ -584,6 +697,18 @@ private System.Windows.Forms.ToolStripMenuItem NotifyMenuStripPlayingSongLabel; private System.Windows.Forms.SplitContainer SplitContainer; private System.Windows.Forms.ToolStripMenuItem makeToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem searchToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem SearchArtistToolStripLabel; + private System.Windows.Forms.ToolStripTextBox SearchArtistsTextBox; + private System.Windows.Forms.ToolStripMenuItem SearchGenresToolStripLabel; + private System.Windows.Forms.ToolStripTextBox SearchGenresTextBox; + private System.Windows.Forms.ToolStripMenuItem ClearArtistSearchButton; + private System.Windows.Forms.ToolStripMenuItem ClearGenreSearchButton; + private System.Windows.Forms.ToolStripMenuItem playbackToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem ShuffleSongButton; + private System.Windows.Forms.ToolStripMenuItem LoopSongButton; + private System.Windows.Forms.ToolStripMenuItem PlayNextSongButton; + private System.Windows.Forms.ToolStripSeparator toolStripSeparator3; } } diff --git a/MusicPlayer/MusicPlayer/MainForm.cs b/MusicPlayer/MusicPlayer/MainForm.cs index 051d12f..7dde8c8 100644 --- a/MusicPlayer/MusicPlayer/MainForm.cs +++ b/MusicPlayer/MusicPlayer/MainForm.cs @@ -19,6 +19,7 @@ namespace MusicPlayer public partial class MainForm : Form { NotificationPopup p; + bool songFinished; public Main main { @@ -47,6 +48,7 @@ namespace MusicPlayer }; p = new NotificationPopup(this); + songFinished = false; } private void MainForm_Load(object sender, EventArgs e) @@ -69,6 +71,11 @@ namespace MusicPlayer main.audio.Stop(); } + public void SongFinished() + { + songFinished = true; + } + private void UpdateTimer_Tick(object sender, EventArgs e) { //Trackbar @@ -139,6 +146,55 @@ namespace MusicPlayer StopButton.Enabled = true; NotifyMenuStripStopButton.Enabled = true; } + + if(PlayNextSongButton.Checked) + { + ShuffleSongButton.Enabled = true; + } + else + { + ShuffleSongButton.Enabled = false; + ShuffleSongButton.Checked = false; + } + + + if (songFinished) + { + Console.WriteLine("Song finished"); + Thread.Sleep(20); + + if (PlayNextSongButton.Checked) + { + Console.WriteLine("Playing next song"); + + int selected = 0; + + if(ShuffleSongButton.Checked) + { + Random r = new Random(); + selected = r.Next(0, SongsTableView.Rows.Count); + while (selected == SongsTableView.SelectedRows[0].Index && SongsTableView.Rows.Count > 1) + selected = r.Next(0, SongsTableView.Rows.Count); + } + else + selected = SongsTableView.SelectedRows[0].Index + 1; + + if (selected >= SongsTableView.Rows.Count && LoopSongButton.Checked) + selected = 0; + + Console.WriteLine(selected); + + SongsTableView.CurrentCell = SongsTableView.Rows[selected].Cells[0]; + SongsTableView_CellDoubleClick(this, new DataGridViewCellEventArgs(0, selected)); + + } + else if (LoopSongButton.Checked) + { + main.audio.Play(main.audio.CurrentSong); + } + + songFinished = false; + } } private void GenreListBox_SelectedIndexChanged(object sender, EventArgs e) @@ -284,7 +340,38 @@ namespace MusicPlayer 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; } } }