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

This commit is contained in:
2015-10-30 00:17:21 +01:00
parent c2afd44654
commit 1c8bde4f1e
3 changed files with 98 additions and 43 deletions
+21 -4
View File
@@ -12,16 +12,17 @@ namespace MusicPlayer
{ {
public class AudioHandler 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 enum BufferState { EMPTY, BUFFERING, DONE }
public AudioState AState { get; set; } public AudioState AState { get; set; }
public BufferState BState { get; set; } public BufferState BState { get; set; }
public int Buffered { get { return Math.Min((int)((bufpos / Length) * 100), 100); } } public int Buffered { get { return Math.Min((int)((bufpos / (double)Length) * 100), 100); } }
public int Position { get { return Math.Min((int)((playpos / Length) * 100), 100); } } public int Position { get { return Math.Min((int)((playpos / (double)Length) * 100), 100); } }
public double Length { get; set; } public long Length { get; set; }
private long bufpos = 0; private long bufpos = 0;
private long playpos = 0; private long playpos = 0;
private long seek = 0;
private Stream ms; 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() public void Stop()
{ {
CreateThreads(); CreateThreads();
@@ -108,6 +119,12 @@ namespace MusicPlayer
position = blockAlignedStream.Position; position = blockAlignedStream.Position;
waveOut.Pause(); waveOut.Pause();
} }
if (AState == AudioState.SEEKING)
{
blockAlignedStream.Position = seek - (seek % blockAlignedStream.WaveFormat.BlockAlign);
AState = AudioState.PLAYING;
waveOut.Play();
}
if (AState == AudioState.STOPPED) if (AState == AudioState.STOPPED)
{ {
waveOut.Stop(); waveOut.Stop();
+70 -38
View File
@@ -21,7 +21,7 @@
} }
#region Windows Form Designer generated code #region Windows Form Designer generated code
bool clicked = false;
/// <summary> /// <summary>
/// Required method for Designer support - do not modify /// Required method for Designer support - do not modify
/// the contents of this method with the code editor. /// the contents of this method with the code editor.
@@ -41,18 +41,20 @@
this.saveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.saveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.viewToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.viewToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.ControlsPanel = new System.Windows.Forms.Panel(); 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.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.PauseButton = new System.Windows.Forms.Button();
this.PlayButton = 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(); ((System.ComponentModel.ISupportInitialize)(this.SongsTableView)).BeginInit();
this.MainPanel.SuspendLayout(); this.MainPanel.SuspendLayout();
this.MenuStrip.SuspendLayout(); this.MenuStrip.SuspendLayout();
this.ControlsPanel.SuspendLayout(); this.ControlsPanel.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.PositionTrackBar)).BeginInit();
this.SuspendLayout(); this.SuspendLayout();
// //
// SongsTableView // SongsTableView
@@ -170,6 +172,7 @@
// ControlsPanel // ControlsPanel
// //
this.ControlsPanel.BackColor = System.Drawing.SystemColors.WindowFrame; this.ControlsPanel.BackColor = System.Drawing.SystemColors.WindowFrame;
this.ControlsPanel.Controls.Add(this.PositionTrackBar);
this.ControlsPanel.Controls.Add(this.PositionLabel); this.ControlsPanel.Controls.Add(this.PositionLabel);
this.ControlsPanel.Controls.Add(this.BufferLabel); this.ControlsPanel.Controls.Add(this.BufferLabel);
this.ControlsPanel.Controls.Add(this.PositionBar); this.ControlsPanel.Controls.Add(this.PositionBar);
@@ -183,6 +186,38 @@
this.ControlsPanel.Size = new System.Drawing.Size(784, 119); this.ControlsPanel.Size = new System.Drawing.Size(784, 119);
this.ControlsPanel.TabIndex = 4; 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 // StopButton
// //
this.StopButton.Location = new System.Drawing.Point(203, 13); this.StopButton.Location = new System.Drawing.Point(203, 13);
@@ -213,43 +248,38 @@
this.PlayButton.UseVisualStyleBackColor = true; this.PlayButton.UseVisualStyleBackColor = true;
this.PlayButton.Click += new System.EventHandler(this.PlayButton_Click); 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 // UpdateTimer
// //
this.UpdateTimer.Interval = 600; this.UpdateTimer.Interval = 600;
this.UpdateTimer.Tick += new System.EventHandler(this.UpdateTimer_Tick); 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 // MainForm
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
@@ -270,6 +300,7 @@
this.MenuStrip.PerformLayout(); this.MenuStrip.PerformLayout();
this.ControlsPanel.ResumeLayout(false); this.ControlsPanel.ResumeLayout(false);
this.ControlsPanel.PerformLayout(); this.ControlsPanel.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.PositionTrackBar)).EndInit();
this.ResumeLayout(false); this.ResumeLayout(false);
this.PerformLayout(); this.PerformLayout();
@@ -296,6 +327,7 @@
private System.Windows.Forms.ProgressBar PositionBar; private System.Windows.Forms.ProgressBar PositionBar;
private System.Windows.Forms.ProgressBar BufferBar; private System.Windows.Forms.ProgressBar BufferBar;
private System.Windows.Forms.Timer UpdateTimer; private System.Windows.Forms.Timer UpdateTimer;
private System.Windows.Forms.TrackBar PositionTrackBar;
} }
} }
+6
View File
@@ -62,6 +62,7 @@ namespace MusicPlayer
{ {
BufferBar.Value = main.audio.Buffered; BufferBar.Value = main.audio.Buffered;
PositionBar.Value = main.audio.Position; PositionBar.Value = main.audio.Position;
PositionTrackBar.Value = main.audio.Position;
} }
private void GenreListBox_SelectedIndexChanged(object sender, EventArgs e) private void GenreListBox_SelectedIndexChanged(object sender, EventArgs e)
@@ -85,5 +86,10 @@ namespace MusicPlayer
main.audio.Play(SongsTableView.CurrentRow.DataBoundItem as Song); main.audio.Play(SongsTableView.CurrentRow.DataBoundItem as Song);
} }
private void PositionTrackBar_ValueChanged()
{
main.audio.Seek(PositionTrackBar.Value);
}
} }
} }