-
NAudio library question
Hello guys I am using NAudio and I have this class :
Code:
public class Music
{
private string fileLocation;
private IWavePlayer waveout;
private WaveStream outputStream;
WaveOutEvent audio;
public Drum(string file)
{
this.fileLocation = file;
this.waveout = new WaveOut();
audio = new WaveOutEvent();
}
static WaveStream CreateInputStream(string name)
{
WaveChannel32 inputStream;
if (name.EndsWith(".wav"))
{
WaveStream readerStream = new WaveFileReader(name);
if (readerStream.WaveFormat.Encoding != WaveFormatEncoding.Pcm)
{
readerStream = WaveFormatConversionStream.CreatePcmStream(readerStream);
readerStream = new BlockAlignReductionStream(readerStream);
}
if (readerStream.WaveFormat.BitsPerSample != 16)
{
var format = new WaveFormat(readerStream.WaveFormat.SampleRate, 16, readerStream.WaveFormat.Channels);
readerStream = new WaveFormatConversionStream(format, readerStream);
}
inputStream = new WaveChannel32(readerStream);
}
else
{
throw new InvalidOperationException("Invalid extension");
}
return inputStream;
}
public void Play()
{
this.outputStream = CreateInputStream(this.fileLocation);
audio.Init(outputStream);
audio.Play();
}
}
Sometime when I use the Play void I get this error:
AlreadyAllocated calling waveOutOpen. any idea how to resolve it ?
The other thing I notice is that each time I want to play something I need to open the file read the whole stream and save it in WaveStream, but this way I am wasting the resource and the application memory, any idea how to read it once and then just to use it.