Added more audio handling

This commit is contained in:
2015-10-29 22:27:12 +01:00
parent 6d9af7c614
commit 615934557c
4 changed files with 130 additions and 32 deletions
+92 -30
View File
@@ -12,47 +12,109 @@ namespace MusicPlayer
{
public class AudioHandler
{
public static Stream ms = new MemoryStream();
public enum AudioState { PLAYING, STOPPED, PAUSED }
public AudioState State { get; set; }
public static void PlayMp3FromUrl(string url)
private Stream ms;
private Thread network;
private Thread audio;
private Song CurrentSong;
public AudioHandler()
{
new Thread(delegate (object o)
{
var response = WebRequest.Create(url).GetResponse();
using (var stream = response.GetResponseStream())
{
byte[] buffer = new byte[65536]; // 64KB chunks
int read;
while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
{
var pos = ms.Position;
ms.Position = ms.Length;
ms.Write(buffer, 0, read);
ms.Position = pos;
}
}
}).Start();
ms = new MemoryStream();
network = new Thread(LoadAudio);
audio = new Thread(PlayAudio);
network.IsBackground = true;
audio.IsBackground = true;
State = AudioState.STOPPED;
}
new Thread(delegate (object o)
public void Play(Song s)
{
if (CurrentSong == s)
State = AudioState.PLAYING;
else
{
// Pre-buffering some data to allow NAudio to start playing
while (ms.Length < 65536 * 10)
Thread.Sleep(1000);
State = AudioState.STOPPED;
ms.Position = 0;
using (WaveStream blockAlignedStream = new BlockAlignReductionStream(WaveFormatConversionStream.CreatePcmStream(new Mp3FileReader(ms))))
network = new Thread(LoadAudio);
audio = new Thread(PlayAudio);
CurrentSong = s;
network.Start(s);
audio.Start();
}
}
public void Stop()
{
State = AudioState.STOPPED;
}
public void Pause()
{
State = AudioState.PAUSED;
}
private void PlayAudio()
{
while (ms.Length < 65536 * 10)
Thread.Sleep(1000);
ms.Position = 0;
using (WaveStream blockAlignedStream = new BlockAlignReductionStream(WaveFormatConversionStream.CreatePcmStream(new Mp3FileReader(ms))))
{
using (WaveOut waveOut = new WaveOut(WaveCallbackInfo.FunctionCallback()))
{
using (WaveOut waveOut = new WaveOut(WaveCallbackInfo.FunctionCallback()))
waveOut.Init(blockAlignedStream);
waveOut.Play();
while (waveOut.PlaybackState != PlaybackState.Stopped)
{
waveOut.Init(blockAlignedStream);
waveOut.Play();
while (waveOut.PlaybackState == PlaybackState.Playing)
System.Threading.Thread.Sleep(100);
if(State == AudioState.PAUSED && waveOut.PlaybackState == PlaybackState.Playing)
{
System.Threading.Thread.Sleep(100);
waveOut.Pause();
}
if (State == AudioState.PLAYING && waveOut.PlaybackState == PlaybackState.Paused)
{
waveOut.Play();
}
if (State == AudioState.STOPPED)
{
waveOut.Stop();
}
}
State = AudioState.STOPPED;
}
}).Start();
}
}
private void LoadAudio(object o)
{
Song s = (Song) o;
var response = WebRequest.Create(s.Url).GetResponse();
State = AudioState.PLAYING;
using (var stream = response.GetResponseStream())
{
byte[] buffer = new byte[65536]; // 64KB chunks
int read;
while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
{
var pos = ms.Position;
ms.Position = ms.Length;
ms.Write(buffer, 0, read);
ms.Position = pos;
if(State == AudioState.STOPPED)
{
stream.Close();
return;
}
}
}
}
}
}
+26
View File
@@ -41,6 +41,8 @@
this.viewToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.ControlsPanel = new System.Windows.Forms.Panel();
this.PlayButton = new System.Windows.Forms.Button();
this.PauseButton = new System.Windows.Forms.Button();
this.StopButton = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.SongsTableView)).BeginInit();
this.MainPanel.SuspendLayout();
this.MenuStrip.SuspendLayout();
@@ -158,6 +160,8 @@
// ControlsPanel
//
this.ControlsPanel.BackColor = System.Drawing.SystemColors.WindowFrame;
this.ControlsPanel.Controls.Add(this.StopButton);
this.ControlsPanel.Controls.Add(this.PauseButton);
this.ControlsPanel.Controls.Add(this.PlayButton);
this.ControlsPanel.Dock = System.Windows.Forms.DockStyle.Bottom;
this.ControlsPanel.Location = new System.Drawing.Point(0, 343);
@@ -175,6 +179,26 @@
this.PlayButton.UseVisualStyleBackColor = true;
this.PlayButton.Click += new System.EventHandler(this.PlayButton_Click);
//
// PauseButton
//
this.PauseButton.Location = new System.Drawing.Point(108, 13);
this.PauseButton.Name = "PauseButton";
this.PauseButton.Size = new System.Drawing.Size(75, 23);
this.PauseButton.TabIndex = 1;
this.PauseButton.Text = "Pause";
this.PauseButton.UseVisualStyleBackColor = true;
this.PauseButton.Click += new System.EventHandler(this.PauseButton_Click);
//
// StopButton
//
this.StopButton.Location = new System.Drawing.Point(203, 13);
this.StopButton.Name = "StopButton";
this.StopButton.Size = new System.Drawing.Size(75, 23);
this.StopButton.TabIndex = 2;
this.StopButton.Text = "Stop";
this.StopButton.UseVisualStyleBackColor = true;
this.StopButton.Click += new System.EventHandler(this.StopButton_Click);
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
@@ -213,6 +237,8 @@
private System.Windows.Forms.ToolStripMenuItem viewToolStripMenuItem;
private System.Windows.Forms.Panel ControlsPanel;
private System.Windows.Forms.Button PlayButton;
private System.Windows.Forms.Button StopButton;
private System.Windows.Forms.Button PauseButton;
}
}
+11 -1
View File
@@ -34,12 +34,22 @@ namespace MusicPlayer
private void PlayButton_Click(object sender, EventArgs e)
{
AudioHandler.PlayMp3FromUrl("http://imegumii.nl/music/English/Monstercat/Direct%20-%20Eternity.mp3");
main.audio.Play(new Song("102", "Test", "Test", "Test", main.api));
}
private void SongsTableView_SelectionChanged(object sender, EventArgs e)
{
DataGridViewSelectedRowCollection col = SongsTableView.SelectedRows;
}
private void PauseButton_Click(object sender, EventArgs e)
{
main.audio.Pause();
}
private void StopButton_Click(object sender, EventArgs e)
{
main.audio.Stop();
}
}
}
+1 -1
View File
@@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace MusicPlayer
{
class Song
public class Song
{
public string SongID { get; set; }
public string Name { get; set; }