Jelajahi Sumber

Semi-fix/add seeking. Still a problem when seeking further than ~20% of song.

Kenneth van Ewijk 10 tahun lalu
induk
melakukan
1c8bde4f1e

+ 22 - 5
MusicPlayer/MusicPlayer/AudioHandler.cs

@@ -12,16 +12,17 @@ namespace MusicPlayer
 {
     public class AudioHandler
     {
-        public enum AudioState { PLAYING, WAITING, STOPPED, PAUSED }
+        public enum AudioState { PLAYING, WAITING, STOPPED, PAUSED, SEEKING }
         public enum BufferState { EMPTY, BUFFERING, DONE }
         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; }
+        public int Buffered { get { return Math.Min((int)((bufpos / (double)Length) * 100), 100); } }
+        public int Position { get { return Math.Min((int)((playpos / (double)Length) * 100), 100); } }
+        public long Length { get; set; }
         private long bufpos = 0;
         private long playpos = 0;
+        private long seek = 0;
 
         private Stream ms;
 
@@ -67,6 +68,16 @@ namespace MusicPlayer
             }
         }
 
+        public void Seek(int position)
+        {
+            if (position >= Buffered-1)
+                return;
+
+            seek = Length / 100 * position;
+            AState = AudioState.SEEKING;
+            
+        }
+
         public void Stop()
         {
             CreateThreads();
@@ -108,7 +119,13 @@ namespace MusicPlayer
                             position = blockAlignedStream.Position;
                             waveOut.Pause();
                         }
-                        if(AState == AudioState.STOPPED)
+                        if (AState == AudioState.SEEKING)
+                        {
+                            blockAlignedStream.Position = seek - (seek % blockAlignedStream.WaveFormat.BlockAlign);
+                            AState = AudioState.PLAYING;
+                            waveOut.Play();
+                        }
+                        if (AState == AudioState.STOPPED)
                         {
                             waveOut.Stop();
                         }

+ 70 - 38
MusicPlayer/MusicPlayer/MainForm.Designer.cs

@@ -21,7 +21,7 @@
         }
 
         #region Windows Form Designer generated code
-
+        bool clicked = false;
         /// <summary>
         /// Required method for Designer support - do not modify
         /// the contents of this method with the code editor.
@@ -41,18 +41,20 @@
             this.saveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
             this.viewToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
             this.ControlsPanel = new System.Windows.Forms.Panel();
-            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);
+            this.BufferLabel = new System.Windows.Forms.Label();
+            this.PositionBar = new System.Windows.Forms.ProgressBar();
+            this.BufferBar = new System.Windows.Forms.ProgressBar();
+            this.StopButton = new System.Windows.Forms.Button();
             this.PauseButton = new System.Windows.Forms.Button();
             this.PlayButton = new System.Windows.Forms.Button();
+            this.UpdateTimer = new System.Windows.Forms.Timer(this.components);
+            this.PositionTrackBar = new System.Windows.Forms.TrackBar();
             ((System.ComponentModel.ISupportInitialize)(this.SongsTableView)).BeginInit();
             this.MainPanel.SuspendLayout();
             this.MenuStrip.SuspendLayout();
             this.ControlsPanel.SuspendLayout();
+            ((System.ComponentModel.ISupportInitialize)(this.PositionTrackBar)).BeginInit();
             this.SuspendLayout();
             // 
             // SongsTableView
@@ -170,6 +172,7 @@
             // ControlsPanel
             // 
             this.ControlsPanel.BackColor = System.Drawing.SystemColors.WindowFrame;
+            this.ControlsPanel.Controls.Add(this.PositionTrackBar);
             this.ControlsPanel.Controls.Add(this.PositionLabel);
             this.ControlsPanel.Controls.Add(this.BufferLabel);
             this.ControlsPanel.Controls.Add(this.PositionBar);
@@ -183,6 +186,38 @@
             this.ControlsPanel.Size = new System.Drawing.Size(784, 119);
             this.ControlsPanel.TabIndex = 4;
             // 
+            // 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";
+            // 
+            // 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";
+            // 
+            // 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;
+            // 
+            // 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;
+            // 
             // StopButton
             // 
             this.StopButton.Location = new System.Drawing.Point(203, 13);
@@ -213,43 +248,38 @@
             this.PlayButton.UseVisualStyleBackColor = true;
             this.PlayButton.Click += new System.EventHandler(this.PlayButton_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);
             // 
+            // PositionTrackBar
+            // 
+            this.PositionTrackBar.Location = new System.Drawing.Point(346, 13);
+            this.PositionTrackBar.Maximum = 100;
+            this.PositionTrackBar.Name = "PositionTrackBar";
+            this.PositionTrackBar.Size = new System.Drawing.Size(426, 45);
+            this.PositionTrackBar.TabIndex = 7;
+            
+            PositionTrackBar.Scroll += (s,e) =>
+            {
+                if (clicked)
+                    return;
+                this.PositionTrackBar_ValueChanged();
+            };
+            PositionTrackBar.MouseDown += (s,e) =>
+            {
+                clicked = true;
+            };
+            PositionTrackBar.MouseUp += (s,e) =>
+            {
+                if (!clicked)
+                    return;
+
+                clicked = false;
+                this.PositionTrackBar_ValueChanged();
+            };
+            // 
             // MainForm
             // 
             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
@@ -270,6 +300,7 @@
             this.MenuStrip.PerformLayout();
             this.ControlsPanel.ResumeLayout(false);
             this.ControlsPanel.PerformLayout();
+            ((System.ComponentModel.ISupportInitialize)(this.PositionTrackBar)).EndInit();
             this.ResumeLayout(false);
             this.PerformLayout();
 
@@ -296,6 +327,7 @@
         private System.Windows.Forms.ProgressBar PositionBar;
         private System.Windows.Forms.ProgressBar BufferBar;
         private System.Windows.Forms.Timer UpdateTimer;
+        private System.Windows.Forms.TrackBar PositionTrackBar;
     }
 }
 

+ 6 - 0
MusicPlayer/MusicPlayer/MainForm.cs

@@ -62,6 +62,7 @@ namespace MusicPlayer
         {
             BufferBar.Value = main.audio.Buffered;
             PositionBar.Value = main.audio.Position;
+            PositionTrackBar.Value = main.audio.Position;
         }
 
         private void GenreListBox_SelectedIndexChanged(object sender, EventArgs e)
@@ -85,5 +86,10 @@ namespace MusicPlayer
             main.audio.Play(SongsTableView.CurrentRow.DataBoundItem as Song);
 
         }
+
+        private void PositionTrackBar_ValueChanged()
+        {
+            main.audio.Seek(PositionTrackBar.Value);
+        }
     }
 }