I'm hearing a brief click at the end of each wave file. My program is an OpenGL game engine running from within a console window and the answer to this unrelated question recommends using function callbacks when using NAudio inside a console app so perhaps the problem is routed there I don't know. Any ideas?
My usage is:
Audio.play("Music.wav");
Audio class:
public class Audio
{
private NAudio.Wave.BlockAlignReductionStream stream = null;
private NAudio.Wave.DirectSoundOut output = null;
public static void Play(string file)
{
Audio audio = new Audio();
audio.DisposeWave();
string fullPath = @"Audio/" + file;
if (fullPath.EndsWith(".mp3"))
{
NAudio.Wave.WaveStream pcm = NAudio.Wave.WaveFormatConversionStream.CreatePcmStream(new NAudio.Wave.Mp3FileReader(fullPath));
audio.stream = new NAudio.Wave.BlockAlignReductionStream(pcm);
}
else if (fullPath.EndsWith(".wav"))
{
NAudio.Wave.WaveStream pcm = new NAudio.Wave.WaveChannel32(new NAudio.Wave.WaveFileReader(fullPath));
audio.stream = new NAudio.Wave.BlockAlignReductionStream(pcm);
}
else throw new InvalidOperationException("Not a correct audio file type.");
audio.output = new NAudio.Wave.DirectSoundOut();
audio.output.Init(audio.stream);
audio.output.Play();
}
private void DisposeWave()
{
if (output != null)
{
if (output.PlaybackState == NAudio.Wave.PlaybackState.Playing) output.Stop();
output.Dispose();
output = null;
}
if (stream != null)
{
stream.Dispose();
stream = null;
}
}
}
}