瀏覽代碼

Added visuals for buffering and position

Kenneth van Ewijk 10 年之前
父節點
當前提交
4f5aaee125

+ 20 - 5
MusicPlayer/MusicPlayer/AudioHandler.cs

@@ -17,6 +17,12 @@ namespace MusicPlayer
         public AudioState AState { get; set; }
         public BufferState BState { get; set; }
 
+        public int Buffered { get { return Math.Min((int)((bufpos / Length) * 100), 100); } }
+        public int Position { get { return Math.Min((int)((playpos / Length) * 100), 100); } }
+        public double Length { get; set; }
+        private long bufpos = 0;
+        private long playpos = 0;
+
         private Stream ms;
 
         private Thread network;
@@ -42,12 +48,14 @@ namespace MusicPlayer
             network.IsBackground = true;
             audio.IsBackground = true;
 
-            
+            Length = 1;
+            bufpos = 0;
+            playpos = 0;
         }
 
         public void Play(Song s)
         {
-            if (CurrentSong == s)
+            if (CurrentSong == s && AState == AudioState.PAUSED)
                 AState = AudioState.PLAYING;
             else
             {
@@ -85,27 +93,31 @@ namespace MusicPlayer
                 {
                     waveOut.Init(blockAlignedStream);
                     waveOut.Play();
+
                     while (waveOut.PlaybackState != PlaybackState.Stopped)
                     {
                         System.Threading.Thread.Sleep(10);
 
                         if(AState == AudioState.PLAYING && waveOut.PlaybackState == PlaybackState.Paused)
                         {
-                            ms.Position = position;
+                            blockAlignedStream.Position = position;
                             waveOut.Play();
                         }
                         if (AState == AudioState.PAUSED && waveOut.PlaybackState == PlaybackState.Playing)
                         {
-                            position = ms.Position;
+                            position = blockAlignedStream.Position;
                             waveOut.Pause();
                         }
                         if(AState == AudioState.STOPPED)
                         {
                             waveOut.Stop();
                         }
+
+                        playpos = blockAlignedStream.Position;
                     }
 
                     AState = AudioState.STOPPED;
+                    playpos = 0;
                 }
             }
         }
@@ -116,7 +128,7 @@ namespace MusicPlayer
             var response = WebRequest.Create(s.Url).GetResponse();
 
             BState = BufferState.EMPTY;
-
+            Length = response.ContentLength;
             using (var stream = response.GetResponseStream())
             {
                 byte[] buffer = new byte[65536]; // 64KB chunks
@@ -130,6 +142,9 @@ namespace MusicPlayer
                     ms.Position = ms.Length;
                     ms.Write(buffer, 0, read);
                     ms.Position = pos;
+
+
+                    this.bufpos += buffer.Length;
                 }
             }
 

+ 53 - 0
MusicPlayer/MusicPlayer/MainForm.Designer.cs

@@ -28,6 +28,7 @@
         /// </summary>
         private void InitializeComponent()
         {
+            this.components = new System.ComponentModel.Container();
             System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
             this.SongsTableView = new System.Windows.Forms.DataGridView();
             this.GenreListBox = new System.Windows.Forms.ListBox();
@@ -43,6 +44,11 @@
             this.PlayButton = new System.Windows.Forms.Button();
             this.PauseButton = new System.Windows.Forms.Button();
             this.StopButton = new System.Windows.Forms.Button();
+            this.BufferBar = new System.Windows.Forms.ProgressBar();
+            this.PositionBar = new System.Windows.Forms.ProgressBar();
+            this.BufferLabel = new System.Windows.Forms.Label();
+            this.PositionLabel = new System.Windows.Forms.Label();
+            this.UpdateTimer = new System.Windows.Forms.Timer(this.components);
             ((System.ComponentModel.ISupportInitialize)(this.SongsTableView)).BeginInit();
             this.MainPanel.SuspendLayout();
             this.MenuStrip.SuspendLayout();
@@ -160,6 +166,10 @@
             // ControlsPanel
             // 
             this.ControlsPanel.BackColor = System.Drawing.SystemColors.WindowFrame;
+            this.ControlsPanel.Controls.Add(this.PositionLabel);
+            this.ControlsPanel.Controls.Add(this.BufferLabel);
+            this.ControlsPanel.Controls.Add(this.PositionBar);
+            this.ControlsPanel.Controls.Add(this.BufferBar);
             this.ControlsPanel.Controls.Add(this.StopButton);
             this.ControlsPanel.Controls.Add(this.PauseButton);
             this.ControlsPanel.Controls.Add(this.PlayButton);
@@ -199,6 +209,43 @@
             this.StopButton.UseVisualStyleBackColor = true;
             this.StopButton.Click += new System.EventHandler(this.StopButton_Click);
             // 
+            // BufferBar
+            // 
+            this.BufferBar.Location = new System.Drawing.Point(13, 60);
+            this.BufferBar.Name = "BufferBar";
+            this.BufferBar.Size = new System.Drawing.Size(265, 23);
+            this.BufferBar.TabIndex = 3;
+            // 
+            // PositionBar
+            // 
+            this.PositionBar.Location = new System.Drawing.Point(13, 90);
+            this.PositionBar.Name = "PositionBar";
+            this.PositionBar.Size = new System.Drawing.Size(265, 23);
+            this.PositionBar.TabIndex = 4;
+            // 
+            // BufferLabel
+            // 
+            this.BufferLabel.AutoSize = true;
+            this.BufferLabel.Location = new System.Drawing.Point(285, 60);
+            this.BufferLabel.Name = "BufferLabel";
+            this.BufferLabel.Size = new System.Drawing.Size(35, 13);
+            this.BufferLabel.TabIndex = 5;
+            this.BufferLabel.Text = "Buffer";
+            // 
+            // PositionLabel
+            // 
+            this.PositionLabel.AutoSize = true;
+            this.PositionLabel.Location = new System.Drawing.Point(285, 90);
+            this.PositionLabel.Name = "PositionLabel";
+            this.PositionLabel.Size = new System.Drawing.Size(44, 13);
+            this.PositionLabel.TabIndex = 6;
+            this.PositionLabel.Text = "Position";
+            // 
+            // UpdateTimer
+            // 
+            this.UpdateTimer.Interval = 600;
+            this.UpdateTimer.Tick += new System.EventHandler(this.UpdateTimer_Tick);
+            // 
             // MainForm
             // 
             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
@@ -218,6 +265,7 @@
             this.MenuStrip.ResumeLayout(false);
             this.MenuStrip.PerformLayout();
             this.ControlsPanel.ResumeLayout(false);
+            this.ControlsPanel.PerformLayout();
             this.ResumeLayout(false);
             this.PerformLayout();
 
@@ -239,6 +287,11 @@
         private System.Windows.Forms.Button PlayButton;
         private System.Windows.Forms.Button StopButton;
         private System.Windows.Forms.Button PauseButton;
+        private System.Windows.Forms.Label PositionLabel;
+        private System.Windows.Forms.Label BufferLabel;
+        private System.Windows.Forms.ProgressBar PositionBar;
+        private System.Windows.Forms.ProgressBar BufferBar;
+        private System.Windows.Forms.Timer UpdateTimer;
     }
 }
 

+ 13 - 2
MusicPlayer/MusicPlayer/MainForm.cs

@@ -17,6 +17,8 @@ namespace MusicPlayer
 {
     public partial class MainForm : Form
     {
+        Song testsong;
+
         public Main main
         {
             get; set;
@@ -29,12 +31,15 @@ namespace MusicPlayer
 
         private void MainForm_Load(object sender, EventArgs e)
         {
-            
+            UpdateTimer.Start();
         }
 
         private void PlayButton_Click(object sender, EventArgs e)
         {
-            main.audio.Play(new Song("102", "Test", "Test", "Test", main.api));
+            if (testsong == null)
+                testsong = new Song("917", "Test", "Test", "Test", main.api);
+
+            main.audio.Play(testsong);
         }
 
         private void SongsTableView_SelectionChanged(object sender, EventArgs e)
@@ -51,5 +56,11 @@ namespace MusicPlayer
         {
             main.audio.Stop();
         }
+
+        private void UpdateTimer_Tick(object sender, EventArgs e)
+        {
+            BufferBar.Value = main.audio.Buffered;
+            PositionBar.Value = main.audio.Position;
+        }
     }
 }

+ 3 - 0
MusicPlayer/MusicPlayer/MainForm.resx

@@ -120,6 +120,9 @@
   <metadata name="MenuStrip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
     <value>118, 17</value>
   </metadata>
+  <metadata name="UpdateTimer.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
+    <value>230, 22</value>
+  </metadata>
   <assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
   <data name="$this.Icon" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
     <value>