我正在使用NAudio,我现在正在玩。
现在,我有一个问题。
我将麦克风录制到内存流中。
当我点击一个按钮时,它会停止录制(以防止同时写入和读取) 并从内存流中发挥。
但我不知道如何告诉它,如果它已经到了最后,如果应该重新开始。
所以在播放之后,它会再次录制我的麦克风,我可以重做这个过程。
由于
private void play_Click(object sender, EventArgs e)
{
StreamReader streamu = new StreamReader(waveStream);
//waveWriter.Close();
sourceStream.StopRecording();
// Wasout.Dispose();
waveStream.Seek(0, SeekOrigin.Begin);
//waveStream.Read(bytes, 0, waveStream.Length);
playwave = new NAudio.Wave.RawSourceWaveStream(@waveStream, sourceStream.WaveFormat);
waveOut2.Init(playwave);
//Aut.InitRecordAndPlayback(playwave, 2, 48000);
waveOut2.Play();
if (streamu.EndOfStream)
{
waveOut2.Dispose();
waveOut2 = new NAudio.Wave.DirectSoundOut();
sourceStream.StartRecording();
Wasout.Init(waveIn);
Wasout.Play();
}
}
这就是我现在所做的,正如你所看到的,我正在搞乱。 好吧,它不起作用; P
答案 0 :(得分:0)
创建一个LoopStream,就像来自NAudioDemo的LoopStream一样,并将RawSourceWaveStream传递给它。 (可能会简化一点,但演示了基本的想法)
class LoopStream : WaveStream
{
WaveStream sourceStream;
public LoopStream(WaveStream source)
{
this.sourceStream = source;
}
public override WaveFormat WaveFormat
{
get { return sourceStream.WaveFormat; }
}
public override long Length
{
get { return long.MaxValue / 32; }
}
public override long Position
{
get
{
return sourceStream.Position;
}
set
{
sourceStream.Position = value;
}
}
public override bool HasData(int count)
{
// infinite loop
return true;
}
public override int Read(byte[] buffer, int offset, int count)
{
int read = 0;
while (read < count)
{
int required = count - read;
int readThisTime = sourceStream.Read(buffer, offset + read, required);
if (readThisTime < required)
{
sourceStream.Position = 0;
}
if (sourceStream.Position >= sourceStream.Length)
{
sourceStream.Position = 0;
}
read += readThisTime;
}
return read;
}
protected override void Dispose(bool disposing)
{
sourceStream.Dispose();
base.Dispose(disposing);
}
}