From 4f5aaee1252601d1606df9363fc50889fead49f0 Mon Sep 17 00:00:00 2001 From: Kenneth van Ewijk Date: Thu, 29 Oct 2015 23:43:42 +0100 Subject: [PATCH] Added visuals for buffering and position --- MusicPlayer/MusicPlayer/AudioHandler.cs | 25 +++++++-- MusicPlayer/MusicPlayer/MainForm.Designer.cs | 53 ++++++++++++++++++++ MusicPlayer/MusicPlayer/MainForm.cs | 15 +++++- MusicPlayer/MusicPlayer/MainForm.resx | 3 ++ 4 files changed, 89 insertions(+), 7 deletions(-) diff --git a/MusicPlayer/MusicPlayer/AudioHandler.cs b/MusicPlayer/MusicPlayer/AudioHandler.cs index 8b2f9d7..72fa6a1 100644 --- a/MusicPlayer/MusicPlayer/AudioHandler.cs +++ b/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; } } diff --git a/MusicPlayer/MusicPlayer/MainForm.Designer.cs b/MusicPlayer/MusicPlayer/MainForm.Designer.cs index 3f00825..69aec45 100644 --- a/MusicPlayer/MusicPlayer/MainForm.Designer.cs +++ b/MusicPlayer/MusicPlayer/MainForm.Designer.cs @@ -28,6 +28,7 @@ /// 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; } } diff --git a/MusicPlayer/MusicPlayer/MainForm.cs b/MusicPlayer/MusicPlayer/MainForm.cs index 1a8cae3..81d0438 100644 --- a/MusicPlayer/MusicPlayer/MainForm.cs +++ b/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; + } } } diff --git a/MusicPlayer/MusicPlayer/MainForm.resx b/MusicPlayer/MusicPlayer/MainForm.resx index 2bbeee3..5faed31 100644 --- a/MusicPlayer/MusicPlayer/MainForm.resx +++ b/MusicPlayer/MusicPlayer/MainForm.resx @@ -120,6 +120,9 @@ 118, 17 + + 230, 22 +