WaveFileWriter を使用して以下の例外に苦労しています。この例外は断続的に発生します。ほとんどの場合、これで問題なく動作します。以下のコード スニペット:
MemoryStream s = new MemoryStream();
MemoryStream newStream = new MemoryStream();
int length = 0;
byte[] buffer = null;
int read = 0;
MixingSampleProvider mixer2 = new MixingSampleProvider(_samples);
SampleToWaveProvider16 mixer3 = new SampleToWaveProvider16(mixer2);
length = mixer3.WaveFormat.AverageBytesPerSecond*Convert.ToInt32(position.TotalSeconds); //position is a Timespan calculated to be the total duration of the WAV files
buffer = new byte[length];
WaveFileWriter waveFileWriter = new WaveFileWriter(new IgnoreDisposeStream(s), mixer3.WaveFormat);
while ((read = mixer3.Read(buffer, 0, buffer.Length)) > 0)
{
waveFileWriter.Write(buffer, 0, read);
}
waveFileWriter.Flush();
waveFileWriter.Close();
waveFileWriter.Dispose();
s.WriteTo(newStream);
例外の詳細は次のとおりです。
System.IndexOutOfRangeException was caught
HResult=-2146233080
Message=Index was outside the bounds of the array.
Source=NAudio
StackTrace:
at NAudio.Wave.SampleProviders.Pcm16BitToSampleProvider.Read(Single[] buffer, Int32 offset, Int32 count)
at NAudio.Wave.SampleProviders.OffsetSampleProvider.Read(Single[] buffer, Int32 offset, Int32 count)
at NAudio.Wave.SampleProviders.MixingSampleProvider.Read(Single[] buffer, Int32 offset, Int32 count)
at NAudio.Wave.SampleProviders.OffsetSampleProvider.Read(Single[] buffer, Int32 offset, Int32 count)
at NAudio.Wave.SampleProviders.MixingSampleProvider.Read(Single[] buffer, Int32 offset, Int32 count)
at NAudio.Wave.SampleProviders.SampleToWaveProvider16.Read(Byte[] destBuffer, Int32 offset, Int32 numBytes)
at GamedayRadio.HalfInning.Process() in xxxxxx
InnerException: