From 9f64137e687d23371e50c729dc7535c662e0d8bd Mon Sep 17 00:00:00 2001 From: Kenneth van Ewijk Date: Thu, 29 Oct 2015 20:45:23 +0100 Subject: [PATCH] Added new stuff --- MusicPlayer/MusicPlayer/AudioHandler.cs | 58 ++++++++++++++++++++ MusicPlayer/MusicPlayer/MainForm.Designer.cs | 34 ++++++++---- MusicPlayer/MusicPlayer/MainForm.cs | 43 +-------------- MusicPlayer/MusicPlayer/MusicPlayer.csproj | 1 + MusicPlayer/MusicPlayer/Song.cs | 19 +++++++ 5 files changed, 102 insertions(+), 53 deletions(-) create mode 100644 MusicPlayer/MusicPlayer/AudioHandler.cs diff --git a/MusicPlayer/MusicPlayer/AudioHandler.cs b/MusicPlayer/MusicPlayer/AudioHandler.cs new file mode 100644 index 0000000..c7065b8 --- /dev/null +++ b/MusicPlayer/MusicPlayer/AudioHandler.cs @@ -0,0 +1,58 @@ +using NAudio.Wave; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Net; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace MusicPlayer +{ + class AudioHandler + { + public static Stream ms = new MemoryStream(); + + public static void PlayMp3FromUrl(string url) + { + new Thread(delegate (object o) + { + var response = WebRequest.Create(url).GetResponse(); + using (var stream = response.GetResponseStream()) + { + byte[] buffer = new byte[65536]; // 64KB chunks + int read; + while ((read = stream.Read(buffer, 0, buffer.Length)) > 0) + { + var pos = ms.Position; + ms.Position = ms.Length; + ms.Write(buffer, 0, read); + ms.Position = pos; + } + } + }).Start(); + + new Thread(delegate (object o) + { + // Pre-buffering some data to allow NAudio to start playing + while (ms.Length < 65536 * 10) + Thread.Sleep(1000); + + ms.Position = 0; + using (WaveStream blockAlignedStream = new BlockAlignReductionStream(WaveFormatConversionStream.CreatePcmStream(new Mp3FileReader(ms)))) + { + using (WaveOut waveOut = new WaveOut(WaveCallbackInfo.FunctionCallback())) + { + waveOut.Init(blockAlignedStream); + waveOut.Play(); + while (waveOut.PlaybackState == PlaybackState.Playing) + { + System.Threading.Thread.Sleep(100); + } + } + } + }).Start(); + } + } +} diff --git a/MusicPlayer/MusicPlayer/MainForm.Designer.cs b/MusicPlayer/MusicPlayer/MainForm.Designer.cs index 1b07509..f851d8e 100644 --- a/MusicPlayer/MusicPlayer/MainForm.Designer.cs +++ b/MusicPlayer/MusicPlayer/MainForm.Designer.cs @@ -38,11 +38,12 @@ this.openToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.saveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.viewToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); - this.playToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.ControlsPanel = new System.Windows.Forms.Panel(); + this.PlayButton = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.SongsTableView)).BeginInit(); this.MainPanel.SuspendLayout(); this.MenuStrip.SuspendLayout(); + this.ControlsPanel.SuspendLayout(); this.SuspendLayout(); // // SongsTableView @@ -54,6 +55,7 @@ | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.SongsTableView.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill; + this.SongsTableView.BackgroundColor = System.Drawing.SystemColors.Control; this.SongsTableView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; this.SongsTableView.Location = new System.Drawing.Point(12, 153); this.SongsTableView.MultiSelect = false; @@ -66,6 +68,7 @@ // // GenreListBox // + this.GenreListBox.BackColor = System.Drawing.SystemColors.Control; this.GenreListBox.FormattingEnabled = true; this.GenreListBox.Location = new System.Drawing.Point(12, 12); this.GenreListBox.Name = "GenreListBox"; @@ -77,6 +80,7 @@ // this.AlbumListView.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); + this.AlbumListView.BackColor = System.Drawing.SystemColors.Control; this.AlbumListView.Location = new System.Drawing.Point(272, 12); this.AlbumListView.Name = "AlbumListView"; this.AlbumListView.Size = new System.Drawing.Size(500, 134); @@ -86,6 +90,7 @@ // // ArtistListBox // + this.ArtistListBox.BackColor = System.Drawing.SystemColors.Control; this.ArtistListBox.FormattingEnabled = true; this.ArtistListBox.Location = new System.Drawing.Point(142, 12); this.ArtistListBox.Name = "ArtistListBox"; @@ -98,6 +103,7 @@ this.MainPanel.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); + this.MainPanel.BackColor = System.Drawing.SystemColors.Window; this.MainPanel.Controls.Add(this.GenreListBox); this.MainPanel.Controls.Add(this.ArtistListBox); this.MainPanel.Controls.Add(this.AlbumListView); @@ -109,6 +115,7 @@ // // MenuStrip // + this.MenuStrip.BackColor = System.Drawing.SystemColors.Window; this.MenuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.fileToolStripMenuItem, this.viewToolStripMenuItem}); @@ -141,28 +148,30 @@ // // viewToolStripMenuItem // - this.viewToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { - this.playToolStripMenuItem}); this.viewToolStripMenuItem.Name = "viewToolStripMenuItem"; this.viewToolStripMenuItem.Size = new System.Drawing.Size(44, 20); this.viewToolStripMenuItem.Text = "View"; // - // playToolStripMenuItem - // - this.playToolStripMenuItem.Name = "playToolStripMenuItem"; - this.playToolStripMenuItem.Size = new System.Drawing.Size(96, 22); - this.playToolStripMenuItem.Text = "Play"; - this.playToolStripMenuItem.Click += new System.EventHandler(this.playToolStripMenuItem_Click); - // // ControlsPanel // - this.ControlsPanel.BackColor = System.Drawing.SystemColors.HotTrack; + this.ControlsPanel.BackColor = System.Drawing.SystemColors.WindowFrame; + this.ControlsPanel.Controls.Add(this.PlayButton); this.ControlsPanel.Dock = System.Windows.Forms.DockStyle.Bottom; this.ControlsPanel.Location = new System.Drawing.Point(0, 343); this.ControlsPanel.Name = "ControlsPanel"; this.ControlsPanel.Size = new System.Drawing.Size(784, 119); this.ControlsPanel.TabIndex = 4; // + // PlayButton + // + this.PlayButton.Location = new System.Drawing.Point(12, 13); + this.PlayButton.Name = "PlayButton"; + this.PlayButton.Size = new System.Drawing.Size(75, 23); + this.PlayButton.TabIndex = 0; + this.PlayButton.Text = "Play"; + this.PlayButton.UseVisualStyleBackColor = true; + this.PlayButton.Click += new System.EventHandler(this.PlayButton_Click); + // // MainForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); @@ -179,6 +188,7 @@ this.MainPanel.ResumeLayout(false); this.MenuStrip.ResumeLayout(false); this.MenuStrip.PerformLayout(); + this.ControlsPanel.ResumeLayout(false); this.ResumeLayout(false); this.PerformLayout(); @@ -196,8 +206,8 @@ private System.Windows.Forms.ToolStripMenuItem openToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem saveToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem viewToolStripMenuItem; - private System.Windows.Forms.ToolStripMenuItem playToolStripMenuItem; private System.Windows.Forms.Panel ControlsPanel; + private System.Windows.Forms.Button PlayButton; } } diff --git a/MusicPlayer/MusicPlayer/MainForm.cs b/MusicPlayer/MusicPlayer/MainForm.cs index d4d88cd..62055d8 100644 --- a/MusicPlayer/MusicPlayer/MainForm.cs +++ b/MusicPlayer/MusicPlayer/MainForm.cs @@ -55,48 +55,9 @@ namespace MusicPlayer table.Add(new Song("Test Song 2", "Test Album 2", "Test Artist 2")); } - private void playToolStripMenuItem_Click(object sender, EventArgs e) + private void PlayButton_Click(object sender, EventArgs e) { - PlayMp3FromUrl("http://imegumii.nl/music/English/Monstercat/Direct%20-%20Eternity.mp3"); - } - - private Stream ms = new MemoryStream(); - public void PlayMp3FromUrl(string url) - { - new Thread(delegate (object o) - { - var response = WebRequest.Create(url).GetResponse(); - using (var stream = response.GetResponseStream()) - { - byte[] buffer = new byte[65536]; // 64KB chunks - int read; - while ((read = stream.Read(buffer, 0, buffer.Length)) > 0) - { - var pos = ms.Position; - ms.Position = ms.Length; - ms.Write(buffer, 0, read); - ms.Position = pos; - } - } - }).Start(); - - // Pre-buffering some data to allow NAudio to start playing - while (ms.Length < 65536 * 10) - Thread.Sleep(1000); - - ms.Position = 0; - using (WaveStream blockAlignedStream = new BlockAlignReductionStream(WaveFormatConversionStream.CreatePcmStream(new Mp3FileReader(ms)))) - { - using (WaveOut waveOut = new WaveOut(WaveCallbackInfo.FunctionCallback())) - { - waveOut.Init(blockAlignedStream); - waveOut.Play(); - while (waveOut.PlaybackState == PlaybackState.Playing) - { - System.Threading.Thread.Sleep(100); - } - } - } + AudioHandler.PlayMp3FromUrl("http://imegumii.nl/music/English/Monstercat/Direct%20-%20Eternity.mp3"); } } } diff --git a/MusicPlayer/MusicPlayer/MusicPlayer.csproj b/MusicPlayer/MusicPlayer/MusicPlayer.csproj index d816bbe..6af1143 100644 --- a/MusicPlayer/MusicPlayer/MusicPlayer.csproj +++ b/MusicPlayer/MusicPlayer/MusicPlayer.csproj @@ -50,6 +50,7 @@ + Form diff --git a/MusicPlayer/MusicPlayer/Song.cs b/MusicPlayer/MusicPlayer/Song.cs index 45db72c..24bb544 100644 --- a/MusicPlayer/MusicPlayer/Song.cs +++ b/MusicPlayer/MusicPlayer/Song.cs @@ -11,12 +11,31 @@ namespace MusicPlayer public string Name { get; set; } public string Album { get; set; } public string Artist { get; set; } + public string Url { get { return GetURL(); } set { SetURL(value); } } + + private string url; public Song(string name, string album, string artist) { Name = name; Album = album; Artist = artist; + url = ""; + } + + private string GetURL() + { + if (url == "") + { + url = "http://imegumii.nl/music/Old%20Music/Pre-Parade%20-%20Toradora.mp3"; + } + + return url; + } + + private void SetURL(string str) + { + url = str; } } }