从我使用NAudio的MeteringSampleProvider更新VolumeMeter控件看到的所有示例中,我认为我拥有正确的代码,但似乎MeteringSampleProvider以及SampleChannel的事件从未触发。我正在同时写入一个wav文件,当我播放它时,录音效果很好。有任何想法吗?
IWaveIn wavStream;
WaveFileWriter wavWriter;
BufferedWaveProvider bufferedWaveProvider;
SampleChannel sampleChannel;
MeteringSampleProvider meteringSampleProvider;
wavStream = new WaveIn();
wavWriter = new WaveFileWriter(CurrentRecording.Filename, wavStream.WaveFormat);
wavStream.DataAvailable += new EventHandler<WaveInEventArgs>((s2, e2) =>
{
//Add sample to the buffered provider
bufferedWaveProvider.AddSamples(e2.Buffer, 0, e2.BytesRecorded);
//************************************************************
//EDIT: This is how I solved the issue!!!
var tmpBuffer = new float[e2.BytesRecorded];
if (meteringSampleProvider != null)
meteringSampleProvider.Read(tmpBuffer, 0, e2.BytesRecorded);
//END EDIT
//************************************************************
//Write to the wave file
wavWriter.Write(e2.Buffer, 0, e2.BytesRecorded);
});
//Create our Buffered provider
bufferedWaveProvider = new BufferedWaveProvider(wavStream.WaveFormat);
bufferedWaveProvider.DiscardOnBufferOverflow = true;
//Create the sample channel
sampleChannel = new SampleChannel(bufferedWaveProvider);
sampleChannel.PreVolumeMeter += new EventHandler<StreamVolumeEventArgs>((s2, e2) =>
{
Console.WriteLine("PreVolumeMeter");
});
//Create the metering sample provider
meteringSampleProvider = new MeteringSampleProvider(sampleChannel);
meteringSampleProvider.StreamVolume += new EventHandler<StreamVolumeEventArgs>((s2, e2) =>
{
Console.WriteLine("PostVolumeMeter");
});
//Start Recording
wavStream.StartRecording();
最佳答案
您不是从meteringSampleProvider
中读取内容,因此不会引发任何事件。通常,正常情况下当然是调用IWavePlayer
的Read
,但是如果您不播放音频,则需要以其他方式将音频通过管道。