0

我正在使用 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

4

1 回答 1

0

创建一个 LoopStream,就像 NAudioDemo 中的这个一样,然后将您的 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);
    }
}
于 2013-07-19T15:33:37.497 回答