NAudio 不播放 RawSourceWaveStream
NAudio don't play RawSourceWaveStream
我想在录制到文件的同时播放 wave 音频流。
我尝试使用 MemoryStream
和 RawSourceWaveStream
来实现这个结果。
它似乎工作正常,音频已正确记录在文件中,但是当我从流中播放音频时,什么也没有播放。
这是我的源代码,有好心人告诉我问题出在哪里吗?
谢谢。
using System;
using System.Windows.Forms;
using NAudio.Wave;
using NAudio.Wave.SampleProviders;
using static System.Environment;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
WaveFileWriter fileWriter;
WaveOut outputSound;
WaveIn waveSource;
RawSourceWaveStream RSS;
OffsetSampleProvider offsetSampleProvider;
Stream sourceStream;
string fileName = GetFolderPath(SpecialFolder.CommonApplicationData) + "\temp.wav";
public Form1()
{
InitializeComponent();
outputSound = new WaveOut();
waveSource = new WaveIn();
waveSource.WaveFormat = new WaveFormat(8000, 16, 1);
waveSource.DataAvailable += new EventHandler<WaveInEventArgs>(waveSource_DataAvailable);
fileWriter = new WaveFileWriter(fileName, waveSource.WaveFormat);
sourceStream = new MemoryStream();
waveSource.StartRecording();
}
private void waveSource_DataAvailable(object sender, WaveInEventArgs e)
{
fileWriter.Write(e.Buffer, 0, e.BytesRecorded);
sourceStream.Write(e.Buffer, 0, e.BytesRecorded);
}
private void btnPlay_Click(object sender, EventArgs e)
{
RSS = new RawSourceWaveStream(sourceStream, waveSource.WaveFormat);
offsetSampleProvider = new OffsetSampleProvider(RSS.ToSampleProvider());
offsetSampleProvider.SkipOver = TimeSpan.FromMilliseconds(0);
offsetSampleProvider.Take = TimeSpan.FromMilliseconds(3000);
outputSound.Init(offsetSampleProvider);
outputSound.Play();
}
}
}
找到问题所在了
行后:
RSS = new RawSourceWaveStream(sourceStream, waveSource.WaveFormat);
我必须将 RawSourceWaveStream
的位置设置为 0。
RSS.Position = 0;
我想在录制到文件的同时播放 wave 音频流。
我尝试使用 MemoryStream
和 RawSourceWaveStream
来实现这个结果。
它似乎工作正常,音频已正确记录在文件中,但是当我从流中播放音频时,什么也没有播放。
这是我的源代码,有好心人告诉我问题出在哪里吗?
谢谢。
using System;
using System.Windows.Forms;
using NAudio.Wave;
using NAudio.Wave.SampleProviders;
using static System.Environment;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
WaveFileWriter fileWriter;
WaveOut outputSound;
WaveIn waveSource;
RawSourceWaveStream RSS;
OffsetSampleProvider offsetSampleProvider;
Stream sourceStream;
string fileName = GetFolderPath(SpecialFolder.CommonApplicationData) + "\temp.wav";
public Form1()
{
InitializeComponent();
outputSound = new WaveOut();
waveSource = new WaveIn();
waveSource.WaveFormat = new WaveFormat(8000, 16, 1);
waveSource.DataAvailable += new EventHandler<WaveInEventArgs>(waveSource_DataAvailable);
fileWriter = new WaveFileWriter(fileName, waveSource.WaveFormat);
sourceStream = new MemoryStream();
waveSource.StartRecording();
}
private void waveSource_DataAvailable(object sender, WaveInEventArgs e)
{
fileWriter.Write(e.Buffer, 0, e.BytesRecorded);
sourceStream.Write(e.Buffer, 0, e.BytesRecorded);
}
private void btnPlay_Click(object sender, EventArgs e)
{
RSS = new RawSourceWaveStream(sourceStream, waveSource.WaveFormat);
offsetSampleProvider = new OffsetSampleProvider(RSS.ToSampleProvider());
offsetSampleProvider.SkipOver = TimeSpan.FromMilliseconds(0);
offsetSampleProvider.Take = TimeSpan.FromMilliseconds(3000);
outputSound.Init(offsetSampleProvider);
outputSound.Play();
}
}
}
找到问题所在了
行后:
RSS = new RawSourceWaveStream(sourceStream, waveSource.WaveFormat);
我必须将 RawSourceWaveStream
的位置设置为 0。
RSS.Position = 0;