From d22957d2abb77f162ac5ef43a064a4ce056fc046 Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 24 Apr 2017 21:51:52 -0400 Subject: [PATCH] audio player scrubbing --- .../Applications/AudioPlayer.Designer.cs | 15 +++++- ShiftOS.WinForms/Applications/AudioPlayer.cs | 52 +++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/ShiftOS.WinForms/Applications/AudioPlayer.Designer.cs b/ShiftOS.WinForms/Applications/AudioPlayer.Designer.cs index 825413d..6263ff7 100644 --- a/ShiftOS.WinForms/Applications/AudioPlayer.Designer.cs +++ b/ShiftOS.WinForms/Applications/AudioPlayer.Designer.cs @@ -64,6 +64,7 @@ namespace ShiftOS.WinForms.Applications this.clearToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.shuffleToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.removeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.loopToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); ((System.ComponentModel.ISupportInitialize)(this.wpaudio)).BeginInit(); this.toolStripContainer1.ContentPanel.SuspendLayout(); this.toolStripContainer1.TopToolStripPanel.SuspendLayout(); @@ -150,6 +151,9 @@ namespace ShiftOS.WinForms.Applications this.pgplaytime.Tag = "keepbg"; this.pgplaytime.Text = "shiftedProgressBar1"; this.pgplaytime.Value = 0; + this.pgplaytime.MouseDown += new System.Windows.Forms.MouseEventHandler(this.startScrub); + this.pgplaytime.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pgplaytime_MouseMove); + this.pgplaytime.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pgplaytime_MouseUp); // // menuStrip1 // @@ -158,7 +162,8 @@ namespace ShiftOS.WinForms.Applications this.addSongToolStripMenuItem, this.clearToolStripMenuItem, this.shuffleToolStripMenuItem, - this.removeToolStripMenuItem}); + this.removeToolStripMenuItem, + this.loopToolStripMenuItem}); this.menuStrip1.Location = new System.Drawing.Point(0, 0); this.menuStrip1.Name = "menuStrip1"; this.menuStrip1.Size = new System.Drawing.Size(798, 24); @@ -193,6 +198,13 @@ namespace ShiftOS.WinForms.Applications this.removeToolStripMenuItem.Text = "Remove"; this.removeToolStripMenuItem.Click += new System.EventHandler(this.removeToolStripMenuItem_Click); // + // loopToolStripMenuItem + // + this.loopToolStripMenuItem.CheckOnClick = true; + this.loopToolStripMenuItem.Name = "loopToolStripMenuItem"; + this.loopToolStripMenuItem.Size = new System.Drawing.Size(46, 20); + this.loopToolStripMenuItem.Text = "Loop"; + // // AudioPlayer // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); @@ -229,5 +241,6 @@ namespace ShiftOS.WinForms.Applications private System.Windows.Forms.FlowLayoutPanel flcontrols; private System.Windows.Forms.Button btnplay; private Controls.ShiftedProgressBar pgplaytime; + private System.Windows.Forms.ToolStripMenuItem loopToolStripMenuItem; } } diff --git a/ShiftOS.WinForms/Applications/AudioPlayer.cs b/ShiftOS.WinForms/Applications/AudioPlayer.cs index 3edf965..28e1c56 100644 --- a/ShiftOS.WinForms/Applications/AudioPlayer.cs +++ b/ShiftOS.WinForms/Applications/AudioPlayer.cs @@ -160,6 +160,23 @@ namespace ShiftOS.WinForms.Applications })); Thread.Sleep(50); } + if (o.PlaybackState == NAudio.Wave.PlaybackState.Stopped) + { + if (lbtracks.SelectedIndex < lbtracks.Items.Count - 1) + { + this.Invoke(new Action(() => + { + lbtracks.SelectedIndex++; + })); + } + else if(loopToolStripMenuItem.Checked == true) + { + this.Invoke(new Action(() => + { + lbtracks.SelectedIndex = 0; + })); + } + } }).Start(); } @@ -171,6 +188,41 @@ namespace ShiftOS.WinForms.Applications } catch { } } + + bool scrubbing = false; + + private void startScrub(object sender, MouseEventArgs e) + { + scrubbing = true; + } + + static public double linear(double x, double x0, double x1, double y0, double y1) + { + if ((x1 - x0) == 0) + { + return (y0 + y1) / 2; + } + return y0 + (x - x0) * (y1 - y0) / (x1 - x0); + } + + private void pgplaytime_MouseMove(object sender, MouseEventArgs e) + { + if (mp3 != null) + try + { + if (scrubbing) + { + long s_pos = (long)linear(e.X, 0, pgplaytime.Width, 0, (double)mp3.Length); + mp3.Position = s_pos; + } + } + catch { } + } + + private void pgplaytime_MouseUp(object sender, MouseEventArgs e) + { + scrubbing = false; + } } public static class ListExtensions