Skip to content

Commit

Permalink
Most of the player thread fixed.
Browse files Browse the repository at this point in the history
  • Loading branch information
VPKSoft committed Jun 25, 2021
1 parent 8261fbc commit 4fda254
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 180 deletions.
3 changes: 2 additions & 1 deletion AmpPlaybackTest/FormMain.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

81 changes: 79 additions & 2 deletions AmpPlaybackTest/FormMain.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using amp.UtilityClasses;
using NAudio.Vorbis;
Expand All @@ -13,8 +14,33 @@ public partial class FormMain : Form
public FormMain()
{
InitializeComponent();
playerThread = new Thread(PlayerThreadSimulation);
playerThread.Start();
}

private readonly Thread playerThread;

private void PlayerThreadSimulation()
{
while (!stopped)
{
if (!string.IsNullOrWhiteSpace(fileName) && File.Exists(fileName))
{
//Invoke(new MethodInvoker(StartPlayback));
StartPlayback2();

fileName = null;
}

Thread.Sleep(100);
}
}

#region AlternateNAudioPlayBack
private volatile WaveOutEvent outputDevice;
private volatile WaveStream audioFile;
#endregion

#region NAudioPlayBack
/// <summary>
/// The current <see cref="NAudio.Wave.WaveOut"/> class instance for the playback.
Expand All @@ -36,7 +62,15 @@ public FormMain()
/// </summary>
private volatile WaveChannel32 volumeStream;

/// <summary>
/// The file name for the music file to play.
/// </summary>
private volatile string fileName;

/// <summary>
/// A flag indicating whether the playback is stopped.
/// </summary>
private volatile bool stopped;
#endregion

/// <summary>
Expand Down Expand Up @@ -68,13 +102,27 @@ private void CloseWaveOut()
}
}

/// <summary>
/// Stops the playback, cleans and disposes of the objects used for the playback.
/// </summary>
private void CloseWaveOut2()
{
outputDevice?.Stop();
outputDevice?.Dispose();
outputDevice = null;
audioFile?.Dispose();
audioFile = null;
}

private void StartPlayback()
{
CloseWaveOut();
waveOutDevice = new WaveOut
{
DesiredLatency = Program.Settings.LatencyMs,
//DesiredLatency = Program.Settings.LatencyMs,
};


try
{
var (waveStream, memoryStream) = CreateInputStream(fileName);
Expand All @@ -97,6 +145,26 @@ private void StartPlayback()
waveOutDevice.Play();
}

private void StartPlayback2()
{
CloseWaveOut2();
outputDevice = new WaveOutEvent();

if (Path.GetExtension(fileName).Equals(".ogg", StringComparison.InvariantCultureIgnoreCase))
{
audioFile = new NAudio.Vorbis.VorbisWaveReader(fileName);
}
else
{
audioFile = new AudioFileReader(fileName);
}

outputDevice.Init(audioFile);
outputDevice.Play();
}



/// <summary>
/// Creates a <see cref="NAudio.Wave.WaveStream"/> class instance from a give <paramref name="fileName"/>.
/// </summary>
Expand Down Expand Up @@ -221,7 +289,16 @@ private void mnuOpenMusicFile_Click(object sender, EventArgs e)
if (odMusicFile.ShowDialog() == DialogResult.OK)
{
fileName = odMusicFile.FileName;
StartPlayback();
//StartPlayback();
}
}

private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
{
stopped = true;
while (!playerThread.Join(100))
{
Application.DoEvents();
}
}
}
Expand Down
Loading

0 comments on commit 4fda254

Please sign in to comment.