Kenneth van Ewijk 10 жил өмнө
parent
commit
9f64137e68

+ 58 - 0
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();
+        }
+    }
+}

+ 22 - 12
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;
     }
 }
 

+ 2 - 41
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");
         }
     }
 }

+ 1 - 0
MusicPlayer/MusicPlayer/MusicPlayer.csproj

@@ -50,6 +50,7 @@
     <Reference Include="System.Xml" />
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="AudioHandler.cs" />
     <Compile Include="MainForm.cs">
       <SubType>Form</SubType>
     </Compile>

+ 19 - 0
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;
         }
     }
 }